/*
 * 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 flatpickr from"flatpickr/flatpickr.min.js";import{DateTime}from"luxon";import PersistentStorage from"@typo3/backend/storage/persistent.js";import ThrottleEvent from"@typo3/core/event/throttle-event.js";class DateTimePicker{constructor(){this.format=(void 0!==opener?.top?.TYPO3?opener.top:top).TYPO3.settings.DateTimePicker.DateFormat}static formatDateForHiddenField(e,t){return"time"!==t&&"timesec"!==t||(e=e.set({year:1970,month:1,day:1})),e.toISO({suppressMilliseconds:!0})}initialize(e){if(!(e instanceof HTMLInputElement)||void 0!==e.dataset.datepickerInitialized)return;let t=PersistentStorage.get("lang");t?"ch"===t&&(t="zh"):t="default",e.dataset.datepickerInitialized="1",import("flatpickr/locales.js").then((()=>{this.initializeField(e,t)}))}initializeField(e,t){const a=this.getScrollEvent(),i=this.getDateOptions(e);i.locale=t,i.onOpen=[()=>{a.bindTo(document.querySelector(".t3js-module-body"))}],i.onClose=()=>{a.release()};const r=flatpickr(e,i);e.addEventListener("input",(()=>{const e=r._input.value,t=r.parseDate(e);e===r.formatDate(t,r.config.dateFormat)&&r.setDate(e)})),e.addEventListener("keyup",(e=>{"Escape"===e.key&&r.close()})),e.addEventListener("change",(t=>{t.stopImmediatePropagation();const a=t.target,i=e.parentElement.parentElement.querySelector('input[type="hidden"]');if(""!==a.value){const e=a.dataset.dateType,t=DateTime.fromFormat(a.value,a._flatpickr.config.dateFormat,{zone:"utc"});t.isValid?i.value=DateTimePicker.formatDateForHiddenField(t,e):a.value=DateTimePicker.formatDateForHiddenField(DateTime.fromISO(i.value,{zone:"utc"}),e)}else i.value="";a.dispatchEvent(new Event("formengine.dp.change"))}))}getScrollEvent(){return new ThrottleEvent("scroll",(()=>{const e=document.querySelector(".flatpickr-input.active");if(null===e)return;const t=e.getBoundingClientRect(),a=e._flatpickr.calendarContainer.offsetHeight;let i,r;window.innerHeight-t.bottom<a&&t.top>a?(i=t.y-a-2,r="arrowBottom"):(i=t.y+t.height+2,r="arrowTop"),e._flatpickr.calendarContainer.style.top=i+"px",e._flatpickr.calendarContainer.classList.remove("arrowBottom","arrowTop"),e._flatpickr.calendarContainer.classList.add(r)}),15)}getDateOptions(e){const t=this.format,a=e.dataset.dateType,i=new Date,r={allowInput:!0,dateFormat:"",defaultDate:e.value,defaultHour:i.getHours(),defaultMinute:i.getMinutes(),enableSeconds:!1,enableTime:!1,formatDate:(e,t)=>DateTime.fromJSDate(e).toFormat(t),parseDate:(e,t)=>DateTime.fromFormat(e,t).toJSDate(),maxDate:"",minDate:"",minuteIncrement:1,noCalendar:!1,weekNumbers:!0};switch(a){case"datetime":r.dateFormat=t[1],r.enableTime=!0;break;case"date":r.dateFormat=t[0];break;case"time":r.dateFormat="HH:mm",r.enableTime=!0,r.noCalendar=!0;break;case"timesec":r.dateFormat="HH:mm:ss",r.enableSeconds=!0,r.enableTime=!0,r.noCalendar=!0;break;case"year":r.dateFormat="yyyy"}return"undefined"!==e.dataset.dateMindate&&(r.minDate=e.dataset.dateMindate),"undefined"!==e.dataset.dateMaxdate&&(r.maxDate=e.dataset.dateMaxdate),r}}export default new DateTimePicker;