/*
 * 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!
 */
define(["require","exports","flatpickr/flatpickr.min","TYPO3/CMS/Core/DocumentService","moment","./Storage/Persistent","TYPO3/CMS/Core/Event/ThrottleEvent"],(function(e,t,a,i,n,r,o){"use strict";class l{constructor(e){var t;this.fieldSelector=".t3js-datetimepicker",this.format=(void 0!==(null===(t=null===opener||void 0===opener?void 0:opener.top)||void 0===t?void 0:t.TYPO3)?opener.top:top).TYPO3.settings.DateTimePicker.DateFormat,i.ready().then(()=>{this.initialize(e)})}static formatDateForHiddenField(e,t){return"time"!==t&&"timesec"!==t||e.year(1970).month(0).date(1),e.format()}initialize(t){let a;if(t instanceof HTMLElement){if(void 0!==t.dataset.datepickerInitialized)return;a=[t]}else console.warn("Initializing all date pickers globally has been marked as deprecated. Please pass a specific element."),a=Array.from(document.querySelectorAll(t||this.fieldSelector)).filter(e=>void 0===e.dataset.datepickerInitialized);if(0===a.length)return;let i=r.get("lang");i?"ch"===i&&(i="zh"):i="default",a.forEach(t=>{t.dataset.datepickerInitialized="1",e(["flatpickr/locales"],()=>{this.initializeField(t,i)})})}initializeField(e,t){const i=this.getScrollEvent(),r=this.getDateOptions(e);r.locale=t,r.onOpen=[()=>{i.bindTo(document.querySelector(".t3js-module-body"))}],r.onClose=()=>{i.release()};const o=a(e,r);e.addEventListener("input",()=>{const e=o._input.value,t=o.parseDate(e);e===o.formatDate(t,o.config.dateFormat)&&o.setDate(e)}),e.addEventListener("keyup",e=>{"Escape"===e.key&&o.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=n.utc(a.value,a._flatpickr.config.dateFormat);t.isValid()?i.value=l.formatDateForHiddenField(t,e):a.value=l.formatDateForHiddenField(n.utc(i.value),e)}else i.value="";a.dispatchEvent(new Event("formengine.dp.change"))})}getScrollEvent(){return new o("scroll",()=>{const e=document.querySelector(".flatpickr-input.active");if(null===e)return;const t=e.getBoundingClientRect(),a=e._flatpickr.calendarContainer.offsetHeight;let i,n;window.innerHeight-t.bottom<a&&t.top>a?(i=t.y-a-2,n="arrowBottom"):(i=t.y+t.height+2,n="arrowTop"),e._flatpickr.calendarContainer.style.top=i+"px",e._flatpickr.calendarContainer.classList.remove("arrowBottom","arrowTop"),e._flatpickr.calendarContainer.classList.add(n)},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)=>n(e).format(t),parseDate:(e,t)=>n(e,t,!0).toDate(),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="Y"}return"undefined"!==e.dataset.dateMindate&&(r.minDate=e.dataset.dateMindate),"undefined"!==e.dataset.dateMaxdate&&(r.maxDate=e.dataset.dateMaxdate),r}}return new l}));