/*
 * 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 m from"flatpickr";import p from"@typo3/backend/storage/persistent.js";import f from"shortcut-buttons-flatpickr";import{DateTime as u}from"luxon";import D from"@typo3/backend/utility/dom-helper.js";import g from"@typo3/core/event/throttle-event.js";import"@typo3/backend/input/clearable.js";import h from"~labels/core.core";const c="ISO8601_LOCALTIME";class b{constructor(){this.format=(typeof opener?.top?.TYPO3<"u"?opener.top:top).TYPO3.settings.DateConfiguration}initialize(n,i=null){if(!(n instanceof HTMLInputElement)||typeof n.dataset.datepickerInitialized<"u")return;let o=document.documentElement.lang;!o||o==="en"?o="default":o==="ch"&&(o="zh"),n.dataset.datepickerInitialized="1",import("flatpickr/dist/l10n").then(()=>{this.initializeField(n,o,i)})}initializeField(n,i,o=null){const r=this.getDateOptions(n);r.locale=i,o&&(r.appendTo=o);const e=p.get("dateTimeFirstDayOfWeek");if(e!=null&&e!==""){const t=parseInt(e,10)-1;m.l10ns[i].firstDayOfWeek=t,m.l10ns.default.firstDayOfWeek=t}if(o instanceof HTMLElement&&o.localName==="typo3-formengine-element-datetime")r.position=()=>{},r.onOpen=[(t,d,l)=>{l.calendarContainer.hasAttribute("popover")||l.calendarContainer.setAttribute("popover","manual"),l.calendarContainer.showPopover()}],r.onClose=(t,d,l)=>{l.calendarContainer.matches(":popover-open")&&l.calendarContainer.hidePopover()};else{const t=this.getScrollEvent();r.onOpen=[()=>{t.bindTo(D.scrollEventTarget(n))}],r.onClose=()=>{t.release()}}const a=m(n,r);a.altInput instanceof HTMLInputElement&&a.input.addEventListener("typo3:internal:clear",()=>{a.clear()}),a._input.addEventListener("change",t=>{const d=t.target.value,l=a.parseDate(d,a.config.altFormat);a.setDate(l,!0)}),a._input.addEventListener("keyup",t=>{t.key==="Escape"&&a.close()})}getScrollEvent(){return new g("scroll",()=>{const n=document.querySelector(".flatpickr-input.active");if(n===null)return;const i=n.getBoundingClientRect(),o=2,r=n._flatpickr.calendarContainer.offsetHeight,s=window.innerHeight-i.bottom<r&&i.top>r;let a,t;s?(a=i.y-r-o,t="arrowBottom"):(a=i.y+i.height+o,t="arrowTop"),n._flatpickr.calendarContainer.style.top=a+"px",n._flatpickr.calendarContainer.classList.remove("arrowBottom","arrowTop"),n._flatpickr.calendarContainer.classList.add(t)},15)}getDateOptions(n){const i=this.format,o=n.dataset.dateType,r=new Date,e={altFormat:"",allowInput:!0,altInput:!0,ariaDateFormat:"DDDD",dateFormat:c,defaultHour:r.getHours(),defaultMinute:r.getMinutes(),enableSeconds:!1,enableTime:!1,formatDate:(s,a)=>{const t=u.fromJSDate(s);return a===c?t.toISO({suppressMilliseconds:!0,includeOffset:!1}):t.toFormat(a)},parseDate:(s,a)=>{if(a===c){const t=u.fromISO(s);if(!t.isValid)throw new Error("Invalid ISO8601 date: "+s);return t.toJSDate()}return u.fromFormat(s,a).toJSDate()},onReady:(s,a,t)=>{if(t.altInput!==void 0){t.altInput.id=t.input.id,t.input.removeAttribute("id"),t.altInput.setAttribute("autocomplete","off"),t.altInput.clearable(),t.input.dataset.formengineInputName!==void 0&&(t.altInput.dataset.formengineDatepickerRealInputName=t.input.dataset.formengineInputName),t.altInput.form.addEventListener("t3-formengine-postfieldvalidation",l=>{l.detail.field===t.input&&t.altInput.classList.toggle("has-error",!l.detail.isValid)});const d=t.altInput.closest(".form-control-clearable-wrapper");d!==null&&d.insertAdjacentElement("afterend",t.input)}},onChange:(s,a,t)=>{t.input.dispatchEvent(new Event("formengine.dp.change"))},maxDate:"",minDate:"",minuteIncrement:1,noCalendar:!1,showMonths:1,monthSelectorType:o.startsWith("date")?"dropdown":"static",weekNumbers:!0,time_24hr:!Intl.DateTimeFormat(navigator.language,{hour:"numeric"}).resolvedOptions().hour12,plugins:[f({theme:"typo3",button:[{label:h.get("labels.datepicker.today")}],onClick:(s,a)=>{a.setDate(new Date,!0)}})]};switch(o){case"datetime":e.altFormat=i.formats.datetime,e.enableTime=!0;break;case"date":e.altFormat=i.formats.date;break;case"time":e.altFormat="HH:mm",e.enableTime=!0,e.noCalendar=!0;break;case"timesec":e.altFormat="HH:mm:ss",e.enableSeconds=!0,e.enableTime=!0,e.noCalendar=!0;break;case"datetimesec":e.altFormat=i.formats.date+" HH:mm:ss",e.enableSeconds=!0,e.enableTime=!0;break;case"year":e.altFormat="yyyy";break;default:}return n.dataset.dateMinDate!==void 0&&(e.minDate=e.parseDate(n.dataset.dateMinDate,c),e.minDate.setSeconds(0)),n.dataset.dateMaxDate!==void 0&&(e.maxDate=e.parseDate(n.dataset.dateMaxDate,c),e.maxDate.setSeconds(59)),e}}var v=new b;export{v as default};
