// #region Imports
import { directive, PartType } from 'lit/directive.js';
import { AsyncDirectiveBase } from '../Abstracts/AsyncDirectiveBase';
import { TimeAgoIntl } from './Intl/TimeAgoIntl';
// #endregion
/**
 * The `TimeAgoDirective` class transforms a date into a "time ago" string.
 *
 * @private
 */
class TimeAgoDirective extends AsyncDirectiveBase {
    // #region Fields
    _intl;
    _timer = null;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `TimeAgoDirective` class.
     *
     * @public
     */
    constructor(args) {
        super(args);
        this._intl = new TimeAgoIntl();
        if (args.type !== PartType.PROPERTY) {
            throw new Error('The `timeAgo` directive can only be used on properties.');
        }
    }
    // #endregion
    // #region Methods
    render(date) {
        this.removeTimer();
        const time = typeof date === 'string' ? new Date(date).getTime() : date.getTime();
        const seconds = Math.round(Math.abs((new Date().getTime() - time) / 1000));
        const timeToUpdate = this.getSecondsUntilUpdate(seconds) * 1000;
        this._timer = window.setTimeout(() => this.setValue(date), timeToUpdate);
        const minutes = Math.round(seconds / 60);
        const hours = Math.round(minutes / 60);
        const days = Math.round(hours / 24);
        const months = Math.round(days / 30.416);
        const years = Math.round(days / 365);
        if (Number.isNaN(seconds)) {
            return '';
        }
        if (seconds <= 45) {
            return this._intl.fewSecondsLabel;
        }
        if (seconds <= 90) {
            return this._intl.minuteLabel;
        }
        if (minutes <= 45) {
            return this._intl.formatMinutesLabel(minutes);
        }
        if (minutes <= 90) {
            return this._intl.hourLabel;
        }
        if (hours <= 22) {
            return this._intl.formatHoursLabel(hours);
        }
        if (hours <= 36) {
            return this._intl.dayLabel;
        }
        if (days <= 25) {
            return this._intl.formatDaysLabel(days);
        }
        if (days <= 45) {
            return this._intl.monthLabel;
        }
        if (days <= 345) {
            return this._intl.formatMonthsLabel(months);
        }
        if (days <= 545) {
            return this._intl.yearLabel;
        }
        return this._intl.formatYearsLabel(years);
    }
    disconnected() {
        super.disconnected();
        this.removeTimer();
    }
    removeTimer() {
        if (this._timer !== null) {
            window.clearTimeout(this._timer);
            this._timer = null;
        }
    }
    getSecondsUntilUpdate(seconds) {
        const min = 60;
        const hr = min * 60;
        const day = hr * 24;
        if (seconds < min) {
            return 2;
        } // Less than 1 min, update every 2 seconds
        if (seconds < hr) {
            return 30;
        } // Less than 1 hour, update every 30 seconds
        if (seconds < day) {
            return 300;
        } // Less than 1 day, update every 5 minutes
        return 3600; // Update every hour
    }
}
/**
 * @public
 */
export const timeAgo = directive(TimeAgoDirective);
//# sourceMappingURL=TimeAgoDirective.js.map