// #region Imports
import { directive, PartType } from 'lit/directive.js';
import { AsyncDirectiveBase } from '../Abstracts/AsyncDirectiveBase';
import { DateAgoIntl } from './Intl/DateAgoIntl';
// #endregion
/**
 * The `DateAgoDirective` class transforms a date into a "date ago" string.
 *
 * @private
 */
class DateAgoDirective extends AsyncDirectiveBase {
    // #region Fields
    _intl;
    _timer = null;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `DateAgoDirective` class.
     *
     * @public
     */
    constructor(args) {
        super(args);
        this._intl = new DateAgoIntl();
        if (args.type !== PartType.PROPERTY) {
            throw new Error('The `dateAgo` directive can only be used on properties.');
        }
    }
    // #endregion
    // #region Methods
    render(date) {
        this.removeTimer();
        const inputDate = typeof date === 'string' ? new Date(date) : date;
        const now = new Date();
        // Set to start of day for date comparison
        const startOfInputDate = new Date(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate());
        const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
        const daysDifference = Math.round((startOfToday.getTime() - startOfInputDate.getTime()) / (1000 * 60 * 60 * 24));
        // Calculate time until next update (at midnight)
        const tomorrow = new Date(now);
        tomorrow.setHours(24, 0, 0, 0);
        const timeToUpdate = tomorrow.getTime() - now.getTime();
        this._timer = window.setTimeout(() => this.setValue(date), timeToUpdate);
        if (Number.isNaN(daysDifference)) {
            return '';
        }
        if (daysDifference === 0) {
            return this._intl.todayLabel;
        }
        if (daysDifference === 1) {
            return this._intl.yesterdayLabel;
        }
        if (daysDifference <= 6) {
            return this._intl.formatDaysAgoLabel(daysDifference);
        }
        if (daysDifference <= 13) {
            return this._intl.lastWeekLabel;
        }
        if (daysDifference <= 27) {
            const weeks = Math.round(daysDifference / 7);
            return this._intl.formatWeeksAgoLabel(weeks);
        }
        if (daysDifference <= 45) {
            return this._intl.lastMonthLabel;
        }
        if (daysDifference <= 345) {
            const months = Math.round(daysDifference / 30.416);
            return this._intl.formatMonthsAgoLabel(months);
        }
        if (daysDifference <= 545) {
            return this._intl.lastYearLabel;
        }
        const years = Math.round(daysDifference / 365);
        return this._intl.formatYearsAgoLabel(years);
    }
    disconnected() {
        super.disconnected();
        this.removeTimer();
    }
    removeTimer() {
        if (this._timer !== null) {
            window.clearTimeout(this._timer);
            this._timer = null;
        }
    }
}
/**
 * @public
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const dateAgo = directive(DateAgoDirective);
//# sourceMappingURL=DateAgoDirective.js.map