import { DateTime, Info, Settings } from "luxon";
import { useBalConfig } from "../config";
import { dateSeparator } from "./date.helpers";
export class BalDate {
    static setTwoDigitCutoffYear(cutoff = 10) {
        Settings.twoDigitCutoffYear = (new Date().getFullYear() % 100) + cutoff;
    }
    static fromAnyFormat(value) {
        const separator = value.replace(/[0-9]/g, '').charAt(0);
        const config = useBalConfig();
        const locale = (config === null || config === void 0 ? void 0 : config.locale) || 'de-CH';
        const pairs = value.split(separator);
        const length = pairs.length;
        if (length === 2) {
            const year = new Date().getFullYear();
            return new BalDate(DateTime.fromFormat(`${pairs[0]}${separator}${pairs[1]}${separator}${year}`, `d${separator}M${separator}yy`, {
                locale,
            }));
        }
        if (length === 3) {
            if (pairs[2].length === 1) {
                return new BalDate(DateTime.fromFormat(`${pairs[0]}${separator}${pairs[1]}${separator}${2000 + parseInt(pairs[2], 10)}`, `d${separator}M${separator}yy`, {
                    locale,
                }));
            }
            else if (pairs[2] === '') {
                const year = new Date().getFullYear();
                return new BalDate(DateTime.fromFormat(`${pairs[0]}${separator}${pairs[1]}${separator}${year}`, `d${separator}M${separator}yy`, {
                    locale,
                }));
            }
        }
        return new BalDate(DateTime.fromFormat(value, `d${separator}M${separator}yy`, { locale }));
    }
    static fromISO(value) {
        return new BalDate(DateTime.fromISO(value || ''));
    }
    static infoMonths({ format, locale } = {}) {
        const config = useBalConfig();
        return Info.months(format || 'long', { locale: locale || (config === null || config === void 0 ? void 0 : config.locale) || 'de-CH' });
    }
    static infoWeekdays({ format, locale } = {}) {
        const config = useBalConfig();
        const weekdays = Info.weekdays(format, { locale: locale || (config === null || config === void 0 ? void 0 : config.locale) || 'de-CH' });
        return weekdays;
    }
    constructor(dt) {
        this.dt = dt;
    }
    get isValid() {
        return this.dt.isValid;
    }
    toISO() {
        if (this.isValid) {
            return this.dt.toISO();
        }
        return '';
    }
    toISODate() {
        if (this.isValid) {
            return this.dt.toISODate();
        }
        return '';
    }
    toFormat() {
        if (this.isValid) {
            const config = useBalConfig();
            const locale = (config === null || config === void 0 ? void 0 : config.locale) || 'de-CH';
            const separator = dateSeparator(locale);
            return this.dt.toFormat(`dd${separator}MM${separator}yyyy`);
        }
        return '';
    }
    isBefore(isoDate) {
        const date = DateTime.fromISO(isoDate || '');
        if (date.isValid) {
            return this.dt < date;
        }
        return false;
    }
    isBeforeOrEqual(isoDate) {
        const date = DateTime.fromISO(isoDate || '');
        if (date.isValid) {
            return this.dt <= date;
        }
        return false;
    }
    isAfter(isoDate) {
        const date = DateTime.fromISO(isoDate || '');
        if (date.isValid) {
            return this.dt > date;
        }
        return false;
    }
    isAfterOrEqual(isoDate) {
        const date = DateTime.fromISO(isoDate || '');
        if (date.isValid) {
            return this.dt >= date;
        }
        return false;
    }
    get month() {
        if (this.isValid) {
            return this.dt.month;
        }
        return undefined;
    }
    get year() {
        if (this.isValid) {
            return this.dt.year;
        }
        return undefined;
    }
}
BalDate.setTwoDigitCutoffYear();
