import { p as proxyCustomElement, H, d as createEvent, h } from './p-604af0ee.js';
import { d as defineCustomElement$7 } from './p-8eedb895.js';
import { d as defineCustomElement$6 } from './p-b7682164.js';
import { d as defineCustomElement$5 } from './p-cdd41c39.js';
import { d as defineCustomElement$4 } from './p-888b8e69.js';
import { d as defineCustomElement$3 } from './p-0102a31f.js';
import { d as defineCustomElement$2 } from './p-a7c64e98.js';
import { d as defineCustomElement$1 } from './p-921307d1.js';

// Gets the days in a given month
const getMonthDays = (date) => {
    const month = date.getMonth();
    const year = date.getFullYear();
    const daysInMonth = new Date(year, month + 1, 0).getDate();
    return Array.from({ length: daysInMonth }, (_, i) => new Date(year, month, i + 1));
};
// Gets the remaining days from the previous month to display on the calendar
const getDaysFromPreviousMonth = (date) => {
    const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1).getDay();
    const prevMonth = new Date(date.getFullYear(), date.getMonth(), 0); // Last day of previous month
    const dates = [];
    for (let i = 0; i < firstDayOfMonth; i++) {
        dates.unshift(new Date(prevMonth.getFullYear(), prevMonth.getMonth(), prevMonth.getDate() - i));
    }
    return dates;
};
// Gets the initial days from the next month to display on the calendar
const getDaysFromNextMonth = (date) => {
    const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDay();
    const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 1); // First day of next month
    const dates = [];
    for (let i = 0; i < (6 - lastDayOfMonth); i++) {
        dates.push(new Date(nextMonth.getFullYear(), nextMonth.getMonth(), nextMonth.getDate() + i));
    }
    return dates;
};
// const defineds = {
//   startOfWeek: startOfWeek(new Date()),
//   endOfWeek: endOfWeek(new Date()),
//   startOfLastWeek: startOfWeek(addDays(new Date(), -7)),
//   endOfLastWeek: endOfWeek(addDays(new Date(), -7)),
//   startOfToday: startOfDay(new Date()),
//   endOfToday: endOfDay(new Date()),
//   startOfYesterday: startOfDay(addDays(new Date(), -1)),
//   endOfYesterday: endOfDay(addDays(new Date(), -1)),
//   startOfMonth: startOfMonth(new Date()),
//   endOfMonth: endOfMonth(new Date()),
//   startOfLastMonth: startOfMonth(addMonths(new Date(), -1)),
//   endOfLastMonth: endOfMonth(addMonths(new Date(), -1)),
// };
// const staticRangeHandler = {
//   range: {},
//   isSelected(range) {
//     const definedRange = this.range();
//     return (
//       isSameDay(range.startDate, definedRange.startDate) &&
//       isSameDay(range.endDate, definedRange.endDate)
//     );
//   },
// };
const isValidDate = (dateString) => {
    // Match D/M/Y or DD/MM/YYYY format
    const regex = /^(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[0-2])\/\d{4}$/;
    if (!regex.test(dateString))
        return false;
    // Parse and check validity
    const [day, month, year] = dateString.split('/').map(Number);
    const parsedDate = new Date(year, month - 1, day);
    return (parsedDate.getDate() === day &&
        parsedDate.getMonth() === month - 1 &&
        parsedDate.getFullYear() === year);
};
const formatDate = (date) => {
    const day = date.getDate();
    const month = date.getMonth() + 1; // Months are 0-indexed
    const year = date.getFullYear();
    return `${day}/${month}/${year}`;
};
// export function createStaticRanges(ranges) {
//   return ranges.map(range => ({ ...staticRangeHandler, ...range }));
// }
// export const defaultStaticRanges = createStaticRanges([
//   {
//     label: 'Today',
//     range: () => ({
//       startDate: defineds.startOfToday,
//       endDate: defineds.endOfToday,
//     }),
//   },
//   {
//     label: 'Yesterday',
//     range: () => ({
//       startDate: defineds.startOfYesterday,
//       endDate: defineds.endOfYesterday,
//     }),
//   },
//   {
//     label: 'This Week',
//     range: () => ({
//       startDate: defineds.startOfWeek,
//       endDate: defineds.endOfWeek,
//     }),
//   },
//   {
//     label: 'Last Week',
//     range: () => ({
//       startDate: defineds.startOfLastWeek,
//       endDate: defineds.endOfLastWeek,
//     }),
//   },
//   {
//     label: 'This Month',
//     range: () => ({
//       startDate: defineds.startOfMonth,
//       endDate: defineds.endOfMonth,
//     }),
//   },
//   {
//     label: 'Last Month',
//     range: () => ({
//       startDate: defineds.startOfLastMonth,
//       endDate: defineds.endOfLastMonth,
//     }),
//   },
// {
//   label: 'Last 3 Month',
//   range: () => ({
//     startDate: defineds.startOfLastMonth,
//     endDate: defineds.endOfLastMonth,
//   }),
// },
// {
//   label: 'Last 7 Days',
//   range: () => ({
//     startDate: defineds.startOfLastMonth,
//     endDate: defineds.endOfLastMonth,
//   }),
// },
// {
//   label: 'Last 30 Days',
//   range: () => ({
//     startDate: defineds.startOfLastMonth,
//     endDate: defineds.endOfLastMonth,
//   }),
// },
// {
//   label: 'Last 90 Days',
//   range: () => ({
//     startDate: defineds.startOfLastMonth,
//     endDate: defineds.endOfLastMonth,
//   }),
// },
// ]);
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const startOfToday = new Date(today);
startOfToday.setHours(0, 0, 0, 0);
const endOfToday = new Date(today);
endOfToday.setHours(23, 59, 59, 999);
const startOfYesterday = new Date(yesterday);
startOfYesterday.setHours(0, 0, 0, 0);
const endOfYesterday = new Date(yesterday);
endOfYesterday.setHours(23, 59, 59, 999);
const startOfWeek = new Date(today);
startOfWeek.setDate(today.getDate() - today.getDay());
const endOfWeek = new Date(today);
endOfWeek.setDate(today.getDate() + (6 - today.getDay()));
const startOfLastWeek = new Date(startOfWeek);
startOfLastWeek.setDate(startOfWeek.getDate() - 7);
const endOfLastWeek = new Date(endOfWeek);
endOfLastWeek.setDate(endOfWeek.getDate() - 7);
const startOfMonth = new Date(today);
startOfMonth.setDate(1);
const endOfMonth = new Date(today);
endOfMonth.setMonth(endOfMonth.getMonth() + 1, 0);
const startOfLastMonth = new Date(startOfMonth);
startOfLastMonth.setMonth(startOfLastMonth.getMonth() - 1, 1);
const endOfLastMonth = new Date(startOfMonth);
endOfLastMonth.setDate(endOfLastMonth.getDate() - 1);
const startOfLast3Months = new Date(startOfMonth);
startOfLast3Months.setMonth(startOfLast3Months.getMonth() - 3, 1);
const endOfLast3Months = new Date(startOfMonth);
endOfLast3Months.setDate(endOfLast3Months.getDate() - 1);
const startOfLast7Days = new Date(today);
startOfLast7Days.setDate(startOfLast7Days.getDate() - 6);
const startOfLast30Days = new Date(today);
startOfLast30Days.setDate(startOfLast30Days.getDate() - 29);
const startOfLast90Days = new Date(today);
startOfLast90Days.setDate(startOfLast90Days.getDate() - 89);
const defaultStaticRanges = [
    {
        label: 'Today',
        range: () => ({
            startDate: startOfToday,
            endDate: endOfToday,
        }),
    },
    {
        label: 'Yesterday',
        range: () => ({
            startDate: startOfYesterday,
            endDate: endOfYesterday,
        }),
    },
    {
        label: 'This Week',
        range: () => ({
            startDate: startOfWeek,
            endDate: endOfWeek,
        }),
    },
    {
        label: 'Last Week',
        range: () => ({
            startDate: startOfLastWeek,
            endDate: endOfLastWeek,
        }),
    },
    {
        label: 'This Month',
        range: () => ({
            startDate: startOfMonth,
            endDate: endOfMonth,
        }),
    },
    {
        label: 'Last Month',
        range: () => ({
            startDate: startOfLastMonth,
            endDate: endOfLastMonth,
        }),
    },
    {
        label: 'Last 3 Months',
        range: () => ({
            startDate: startOfLast3Months,
            endDate: endOfLast3Months,
        }),
    },
    {
        label: 'Last 7 Days',
        range: () => ({
            startDate: startOfLast7Days,
            endDate: endOfToday,
        }),
    },
    {
        label: 'Last 30 Days',
        range: () => ({
            startDate: startOfLast30Days,
            endDate: endOfToday,
        }),
    },
    {
        label: 'Last 90 Days',
        range: () => ({
            startDate: startOfLast90Days,
            endDate: endOfToday,
        }),
    },
];
function compareDatesWithoutTime(date1, date2) {
    return (date1.getFullYear() === date2.getFullYear() &&
        date1.getMonth() === date2.getMonth() &&
        date1.getDate() === date2.getDate());
}
const validateInputDate = (date) => {
    // Regular expression to match the date format "Month Day, Year"
    const dateFormat = /^(?:January|February|March|April|May|June|July|August|September|October|November|December) (?:0?[1-9]|[12][0-9]|3[01]), (?:19|20)\d{2}$/;
    if (dateFormat.test(date)) {
        // Date format is valid
        console.log("date is valid");
        return true;
    }
    else {
        // Date format is invalid
        console.log("date is not valid");
        return false;
    }
};

const DgaDatepicker = /*@__PURE__*/ proxyCustomElement(class DgaDatepicker extends H {
    constructor() {
        super();
        this.__registerHost();
        this.change = createEvent(this, "change", 7);
        this.initialDate = new Date();
        this.range = false;
        this.dualMonthView = false;
        this.inputField = false;
        this.submitButton = false;
        this.qickOptions = false;
        // @Prop() onChange?: (event?: any) => void = () => { };
        // value;
        // onChange = () => {};
        // @Prop() onSubmit = () => {};
        // @Prop() onCancel = () => {};
        this.onSubmit = () => { };
        this.onCancel = () => { };
        this.currentMonth = this.initialDate;
        // @State() selectedDate: Date | null = null;
        this.selectedDate = this.value || new Date();
        this.rangeDates = {};
        this.inputDates = {
            startDate: '',
            endDate: '',
        };
        this.handlePrevClick = () => {
            const prevMonth = new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() - 1, 1);
            // this.monthChange.emit(prevMonth);
            this.handleChangeMonth(prevMonth);
        };
        this.handleNextClick = () => {
            const nextMonth = new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() + 1, 1);
            // this.monthChange.emit(nextMonth);
            this.handleChangeMonth(nextMonth);
        };
    }
    watchSelectedDate(newDate, oldDate) {
        if (newDate !== oldDate) {
            // this.onChange(newDate);
            this.change.emit(newDate);
        }
    }
    watchValue(newValue, oldValue) {
        if (newValue !== oldValue && newValue != this.selectedDate) {
            this.selectedDate = newValue;
            this.currentMonth = new Date(newValue.getFullYear(), newValue.getMonth(), 1);
        }
    }
    get nextMonth() {
        return new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() + 1);
    }
    get days() {
        return [
            ...getDaysFromPreviousMonth(this.currentMonth).slice(0, 2),
            ...getMonthDays(this.currentMonth),
            ...getDaysFromNextMonth(this.currentMonth).slice(0, 2),
        ];
    }
    get nextMonthDays() {
        return [
            ...getDaysFromPreviousMonth(this.nextMonth).slice(0, 2),
            ...getMonthDays(this.nextMonth),
            ...getDaysFromNextMonth(this.nextMonth).slice(0, 2),
        ];
    }
    formatDate(date) {
        if (!date)
            return '';
        const dateObj = new Date(date);
        const monthNames = [
            'January', 'February', 'March', 'April', 'May', 'June',
            'July', 'August', 'September', 'October', 'November', 'December',
        ];
        return `${monthNames[dateObj.getMonth()]} ${dateObj.getDate()}, ${dateObj.getFullYear()}`;
    }
    handleSelectDate(date) {
        if (this.range) {
            if (!this.rangeDates.startDate) {
                this.rangeDates = { startDate: date };
            }
            else if (!this.rangeDates.endDate) {
                if (date >= this.rangeDates.startDate) {
                    this.rangeDates = Object.assign(Object.assign({}, this.rangeDates), { endDate: date });
                }
                else {
                    this.rangeDates = { startDate: date };
                }
            }
            else {
                this.rangeDates = { startDate: date, endDate: null };
            }
        }
        else {
            this.selectedDate = date;
        }
    }
    handleInputDate(event) {
        const inputElement = event.target;
        const { name, value } = inputElement;
        this.inputDates = Object.assign(Object.assign({}, this.inputDates), { [name]: value });
    }
    handleChangeMonth(newMonth) {
        this.currentMonth = newMonth;
    }
    isMidRange(date) {
        if (this.range && this.rangeDates.startDate && this.rangeDates.endDate) {
            return date > this.rangeDates.startDate && date < this.rangeDates.endDate;
        }
        return false;
    }
    isFirstRangeChild(date) {
        if (this.range && this.rangeDates.startDate && this.rangeDates.endDate) {
            return this.rangeDates.startDate.getTime() !== this.rangeDates.endDate.getTime()
                && date.getTime() === this.rangeDates.startDate.getTime();
        }
        return false;
    }
    isLastRangeChild(date) {
        if (this.range && this.rangeDates.startDate && this.rangeDates.endDate) {
            return this.rangeDates.startDate.getTime() !== this.rangeDates.endDate.getTime()
                && date.getTime() === this.rangeDates.endDate.getTime();
        }
        return false;
    }
    componentDidLoad() {
        if (this.inputField) {
            if (this.rangeDates.startDate) {
                this.inputDates = Object.assign(Object.assign({}, this.inputDates), { startDate: this.formatDate(this.rangeDates.startDate) });
            }
            if (this.rangeDates.endDate) {
                this.inputDates = Object.assign(Object.assign({}, this.inputDates), { endDate: this.formatDate(this.rangeDates.endDate) });
            }
            else {
                this.inputDates = Object.assign(Object.assign({}, this.inputDates), { endDate: '' });
            }
        }
    }
    render() {
        return (h("div", { key: '4baaa26a6c30dcccc021748661f72bd2dae1fb75', class: "date-picker-root dga-flex-column", "data-dual-view": this.dualMonthView }, h("div", { key: '80f484e2cb057556fbf3bad05e57b760a5969abd', class: "flex" }, this.qickOptions && this.range && (h("div", { key: '087f4da4763f55cf628b7c0935527b43c80a3479', class: "date-picker-root__quick-options" }, h("span", { key: '9fee9540b0bbe3707934030c31144b4910b8faee', class: "text-md-regular" }, "Qick options"), h("ul", { key: '2f12a935af2a0a98e388a4d768884b4b08b882d9', class: "options-list" }, defaultStaticRanges.map(({ label, range }) => (h("li", { key: label }, h("button", { class: "text-md-medium", onClick: () => this.rangeDates = range }, label))))))), h("div", { key: '6ca04fd773dcf97e7ac09d9a7a25e7db95942241', class: "date-picker-root__main" }, this.inputField && (h("div", { key: '5e61dffdba97e450d07434d104a187acab072001', class: "date-picker-root__main__inputs" }, h("dga-text-input", { key: '412d9237fe481eea7747ec710634e186d3e673b3', placeholder: "Month DD, YYYY", name: "startDate", value: this.inputDates.startDate, onChange: (e) => this.handleInputDate(e), onBlur: (e) => {
                if (validateInputDate(e.target.value)) {
                    const date = new Date(e.target.value);
                    if (this.range) {
                        if (!this.rangeDates.startDate) {
                            this.rangeDates = { startDate: date };
                        }
                        else if (this.rangeDates.endDate) {
                            if (date >= this.rangeDates.endDate) {
                                this.rangeDates = { startDate: date, endDate: date };
                            }
                            else {
                                this.rangeDates = Object.assign(Object.assign({}, this.rangeDates), { startDate: date });
                            }
                        }
                        else {
                            this.rangeDates = { startDate: date, endDate: null };
                        }
                    }
                    else {
                        this.selectedDate = date;
                    }
                }
            } }), this.range && (h("dga-text-input", { key: 'bba975f4f1b2de408e67448d5c44b9636430828d', placeholder: "Month DD, YYYY", name: "endDate", value: this.inputDates.endDate, onChange: (e) => this.handleInputDate(e), onBlur: (e) => {
                if (validateInputDate(e.target.value)) {
                    const date = new Date(e.target.value);
                    if (this.range) {
                        if (!this.rangeDates.endDate) {
                            if (!this.rangeDates.startDate) {
                                this.rangeDates = { startDate: date, endDate: null };
                            }
                            else if (date <= this.rangeDates.startDate) {
                                this.rangeDates = {
                                    startDate: date,
                                    endDate: this.rangeDates.startDate,
                                };
                            }
                        }
                        else if (this.rangeDates.startDate) {
                            if (date <= this.rangeDates.startDate) {
                                this.rangeDates = {
                                    startDate: date,
                                    endDate: this.rangeDates.startDate,
                                };
                            }
                            else {
                                this.rangeDates = Object.assign(Object.assign({}, this.rangeDates), { endDate: date });
                            }
                        }
                        else {
                            this.rangeDates = { startDate: date, endDate: null };
                        }
                    }
                    else {
                        this.selectedDate = date;
                    }
                }
            } })))), h("div", { key: '45957def834fe05410727802792e4e2769462d7f', class: "date-picker-root__main__content-wrapper" }, h("div", { key: '398fdf3c164f3bf5bdb6227747ad6c6097f715b4', class: "date-picker" }, h("month-navigator", { key: '1cb193a4f5cc3c7af7147796bc90d858a363bb91', currentMonth: this.currentMonth, onChange: (date) => this.handleChangeMonth(date), showArrows: !this.dualMonthView }), h("div", { key: 'b0b7edd4acb4fd62d522f62fa34a9cc8f2753378', class: "date-picker__weekdays" }, ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"].map((day) => (h("div", { key: day, class: "date-picker__weekday" }, day)))), h("div", { key: '38395bc9a6c9fc7cd2454a8f9bd4d45f3f832036', class: "date-picker__date-grid" }, this.days.map((date) => (h("date-cell", { key: date.toString(), date: date, isCurrentMonth: date.getMonth() === this.currentMonth.getMonth(), isSelected: (this.range)
                ? (!!this.rangeDates.startDate &&
                    compareDatesWithoutTime(date, this.rangeDates.startDate)) ||
                    (!!this.rangeDates.endDate &&
                        compareDatesWithoutTime(date, this.rangeDates.endDate))
                : this.selectedDate
                    ? compareDatesWithoutTime(date, this.selectedDate)
                    : false, isToday: new Date().toDateString() === date.toDateString(), isFirstRangeChild: this.isFirstRangeChild(date), isLastRangeChild: this.isLastRangeChild(date), midRange: this.isMidRange(date), onSelect: (date) => this.handleSelectDate(date) }))))), this.dualMonthView && (h("div", { key: '900d8d3c3ba17ca570813efb3f86f0ac7cf632c3', class: "date-picker" }, h("month-navigator", { key: '5b29939a2e66dc7ed187a021f17f8c0ad253802c', currentMonth: this.nextMonth, onChange: (date) => {
                const newMonth = date.getMonth() === this.currentMonth.getMonth()
                    ? new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() - 1)
                    : new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() + 1);
                this.handleChangeMonth(newMonth);
            }, showArrows: true }), h("div", { key: '0745ba842e0a46b4a86a773cf92a4ee4f135f35e', class: "date-picker__weekdays" }, ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"].map((day) => (h("div", { key: day, class: "date-picker__weekday" }, day)))), h("div", { key: '4329675817f4b4909ea5ad050444585820e161fa', class: "date-picker__date-grid" }, this.nextMonthDays.map((date) => (h("date-cell", { key: date.toString(), date: date, isCurrentMonth: date.getMonth() === this.nextMonth.getMonth(), isSelected: this.range
                ? (!!this.rangeDates.startDate &&
                    date.getTime() ===
                        this.rangeDates.startDate.getTime()) ||
                    (!!this.rangeDates.endDate &&
                        date.getTime() === this.rangeDates.endDate.getTime())
                : this.selectedDate
                    ? date.getTime() === this.selectedDate.getTime()
                    : false, isToday: new Date().toDateString() === date.toDateString(), midRange: this.isMidRange(date), isFirstRangeChild: this.isFirstRangeChild(date), isLastRangeChild: this.isLastRangeChild(date), onSelect: (date) => this.handleSelectDate(date) }))))))))), this.submitButton && (h("div", { key: '1fbe152485e879a790d2b9ed014e99c3bfb3025b', class: "date-picker-root__main__buttons" }, h("dga-button", { key: 'd2d542224fd3676dcfb2c540f1dadc19f233bd9c', label: "Cancel", onClick: this.onCancel, size: "lg", variant: "secondary-outline" }), h("dga-button", { key: '28bed2e3bea757b15aa8148ce3b6317c95741e92', label: "Submit", onClick: this.onSubmit, size: "lg", variant: "primary-neutral" })))));
    }
    static get watchers() { return {
        "selectedDate": ["watchSelectedDate"],
        "value": ["watchValue"]
    }; }
}, [0, "dga-datepicker", {
        "initialDate": [16],
        "range": [4],
        "dualMonthView": [4, "dual-month-view"],
        "inputField": [4, "input-field"],
        "submitButton": [4, "submit-button"],
        "qickOptions": [4, "qick-options"],
        "value": [16],
        "onSubmit": [16],
        "onCancel": [16],
        "currentMonth": [32],
        "selectedDate": [32],
        "rangeDates": [32],
        "inputDates": [32]
    }, undefined, {
        "selectedDate": ["watchSelectedDate"],
        "value": ["watchValue"]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["dga-datepicker", "date-cell", "dga-button", "dga-helper-text", "dga-icon", "dga-label", "dga-text-input", "month-navigator"];
    components.forEach(tagName => { switch (tagName) {
        case "dga-datepicker":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, DgaDatepicker);
            }
            break;
        case "date-cell":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "dga-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "dga-helper-text":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "dga-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "dga-label":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "dga-text-input":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "month-navigator":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}

export { DgaDatepicker as D, defineCustomElement as d, formatDate as f, isValidDate as i };

//# sourceMappingURL=p-a1269ead.js.map