/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
import p from"flatpickr";import m from"shortcut-buttons-flatpickr";import{DateTime as c}from"luxon";import f from"@typo3/core/event/throttle-event.js";import"@typo3/backend/input/clearable.js";const d="ISO8601_UTC";class D{constructor(){this.format=(typeof opener?.top?.TYPO3<"u"?opener.top:top).TYPO3.settings.DateTimePicker.DateFormat}initialize(a){if(!(a instanceof HTMLInputElement)||typeof a.dataset.datepickerInitialized<"u")return;let n=document.documentElement.lang;!n||n==="en"?n="default":n==="ch"&&(n="zh"),a.dataset.datepickerInitialized="1",import("flatpickr/dist/l10n").then(()=>{this.initializeField(a,n)})}initializeField(a,n){const s=this.getScrollEvent(),i=this.getDateOptions(a);i.locale=n,i.onOpen=[()=>{s.bindTo(document.querySelector(".t3js-module-body"))}],i.onClose=()=>{s.release()};const t=p(a,i);t.altInput instanceof HTMLInputElement&&t.input.addEventListener("typo3:internal:clear",()=>{t.clear()}),t._input.addEventListener("change",r=>{const o=r.target.value,e=t.parseDate(o,t.config.altFormat);t.setDate(e,!0)}),t._input.addEventListener("keyup",r=>{r.key==="Escape"&&t.close()})}getScrollEvent(){return new f("scroll",()=>{const a=document.querySelector(".flatpickr-input.active");if(a===null)return;const n=a.getBoundingClientRect(),s=2,i=a._flatpickr.calendarContainer.offsetHeight,r=window.innerHeight-n.bottom<i&&n.top>i;let o,e;r?(o=n.y-i-s,e="arrowBottom"):(o=n.y+n.height+s,e="arrowTop"),a._flatpickr.calendarContainer.style.top=o+"px",a._flatpickr.calendarContainer.classList.remove("arrowBottom","arrowTop"),a._flatpickr.calendarContainer.classList.add(e)},15)}getDateOptions(a){const n=this.format,s=a.dataset.dateType,i=new Date,t={altFormat:"",allowInput:!0,altInput:!0,ariaDateFormat:"DDDD",dateFormat:d,defaultHour:i.getHours(),defaultMinute:i.getMinutes(),enableSeconds:!1,enableTime:!1,formatDate:(r,o)=>{const e=c.fromJSDate(r);return o===d?e.toUTC().plus(e.offset*60*1e3).toISO({suppressMilliseconds:!0}):e.toFormat(o)},parseDate:(r,o)=>{if(o===d){const e=c.fromISO(r,{zone:"utc"});if(!e.isValid)throw new Error("Invalid ISO8601 date: "+r);const l=e.toLocal();return l.minus(l.offset*60*1e3).toJSDate()}return c.fromFormat(r,o).toJSDate()},onReady:(r,o,e)=>{if(e.altInput!==void 0){e.altInput.id=e.input.id,e.input.removeAttribute("id"),e.altInput.setAttribute("autocomplete","off"),e.altInput.clearable(),e.input.dataset.formengineInputName!==void 0&&(e.altInput.dataset.formengineDatepickerRealInputName=e.input.dataset.formengineInputName),e.altInput.form.addEventListener("t3-formengine-postfieldvalidation",u=>{u.detail.field===e.input&&e.altInput.classList.toggle("has-error",!u.detail.isValid)});const l=e.altInput.closest(".form-control-clearable-wrapper");l!==null&&l.insertAdjacentElement("afterend",e.input)}},onChange:(r,o,e)=>{e.input.dispatchEvent(new Event("formengine.dp.change"))},maxDate:"",minDate:"",minuteIncrement:1,noCalendar:!1,showMonths:1,monthSelectorType:s.startsWith("date")?"dropdown":"static",weekNumbers:!0,time_24hr:!Intl.DateTimeFormat(navigator.language,{hour:"numeric"}).resolvedOptions().hour12,plugins:[m({theme:"typo3",button:[{label:top.TYPO3.lang["labels.datepicker.today"]||"Today"}],onClick:(r,o)=>{o.setDate(new Date,!0)}})]};switch(s){case"datetime":t.altFormat=n[1],t.enableTime=!0;break;case"date":t.altFormat=n[0];break;case"time":t.altFormat="HH:mm",t.enableTime=!0,t.noCalendar=!0;break;case"timesec":t.altFormat="HH:mm:ss",t.enableSeconds=!0,t.enableTime=!0,t.noCalendar=!0;break;case"year":t.altFormat="yyyy";break;default:}return a.dataset.dateMinDate!==void 0&&(t.minDate=t.parseDate(a.dataset.dateMinDate,d),t.minDate.setSeconds(0)),a.dataset.dateMaxDate!==void 0&&(t.maxDate=t.parseDate(a.dataset.dateMaxDate,d),t.maxDate.setSeconds(59)),t}}var g=new D;export{g as default};
