import * as i0 from '@angular/core';
import { Pipe, NgModule } from '@angular/core';
import * as i1 from 'ng-devui/i18n';
import * as datefns from 'date-fns';
import { Subject, of } from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';

class RelativeTimePipe {
    constructor(i18n) {
        this.i18n = i18n;
        this._destroyed$ = new Subject();
    }
    transform(value, limit, compareDate, weekStartsOn = 1) {
        if (!value) {
            return of('');
        }
        const threshold = {
            month: 3,
            week: 4,
            day: 6,
            hour: 6,
            minute: 59,
            second: 59 // at least 59 seconds using minute.
        };
        if (!datefns.isDate(value)) {
            value = new Date(value);
        }
        const now = compareDate ? new Date(compareDate) : Date.now();
        const startOfYearForTarget = datefns.startOfYear(value);
        const startOfYearForToday = datefns.startOfYear(now);
        const diffYears = datefns.differenceInYears(startOfYearForToday, startOfYearForTarget);
        const absDiffYears = Math.abs(diffYears);
        const startOfMonthForTarget = datefns.startOfMonth(value);
        const startOfMonthForToday = datefns.startOfMonth(now);
        const diffMonths = datefns.differenceInMonths(startOfMonthForToday, startOfMonthForTarget);
        const absDiffMonths = Math.abs(diffMonths);
        const startOfWeekForTarget = datefns.startOfWeek(value, { weekStartsOn });
        const startOfWeekForToday = datefns.startOfWeek(now, { weekStartsOn });
        const diffWeeks = datefns.differenceInWeeks(startOfWeekForToday, startOfWeekForTarget);
        const absDiffWeeks = Math.abs(diffWeeks);
        const startOfDayForTarget = datefns.startOfDay(value);
        const startOfDayForToday = datefns.startOfDay(now);
        const diffDays = datefns.differenceInDays(startOfDayForToday, startOfDayForTarget);
        const absDiffDays = Math.abs(diffDays);
        const diffHours = datefns.differenceInHours(now, value);
        const absDiffHours = Math.abs(diffHours);
        const diffMinutes = datefns.differenceInMinutes(now, value);
        const absDiffMinutes = Math.abs(diffMinutes);
        const diffSeconds = datefns.differenceInSeconds(now, value);
        const absDiffSeconds = Math.abs(diffSeconds);
        return this.i18n.langChange().pipe(map((data) => {
            if (absDiffSeconds > limit) {
                return new Date(value);
            }
            const i18nCommonText = data['relativeTime'];
            if (absDiffYears > 0 && absDiffMonths > threshold.month) {
                return diffYears > 0 ? i18nCommonText.yearsAgo(absDiffYears) : i18nCommonText.yearsLater(absDiffYears);
            }
            else if (absDiffMonths > 0 && absDiffWeeks >= threshold.week) {
                return diffMonths > 0 ? i18nCommonText.monthsAgo(absDiffMonths) : i18nCommonText.monthsLater(absDiffMonths);
            }
            else if (absDiffWeeks > 0 && absDiffDays > threshold.day) {
                return diffWeeks > 0 ? i18nCommonText.weeksAgo(absDiffWeeks) : i18nCommonText.weeksLater(absDiffWeeks);
            }
            else if (absDiffDays > 0 && absDiffHours > threshold.hour) {
                return diffDays > 0 ? i18nCommonText.daysAgo(absDiffDays) : i18nCommonText.daysLater(absDiffDays);
            }
            else if (absDiffHours > 0 && absDiffMinutes > threshold.minute) {
                return absDiffHours + (diffHours > 0 ? i18nCommonText.hoursAgo : i18nCommonText.hoursLater);
            }
            else if (absDiffMinutes > 0 && absDiffSeconds > threshold.second) {
                return absDiffMinutes + (diffMinutes > 0 ? i18nCommonText.minutesAgo : i18nCommonText.minutesLater);
            }
            else if (diffSeconds || diffSeconds === 0) {
                return diffSeconds >= 0 ? i18nCommonText.justnow : i18nCommonText.later;
            }
            else {
                return '';
            }
        }), takeUntil(this._destroyed$));
    }
    ngOnDestroy() {
        this._destroyed$.next();
        this._destroyed$.complete();
    }
}
RelativeTimePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RelativeTimePipe, deps: [{ token: i1.I18nService }], target: i0.ɵɵFactoryTarget.Pipe });
RelativeTimePipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "14.2.0", ngImport: i0, type: RelativeTimePipe, name: "dRelativeTime" });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RelativeTimePipe, decorators: [{
            type: Pipe,
            args: [{
                    name: 'dRelativeTime'
                }]
        }], ctorParameters: function () { return [{ type: i1.I18nService }]; } });

class RelativeTimeModule {
}
RelativeTimeModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RelativeTimeModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
RelativeTimeModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.0", ngImport: i0, type: RelativeTimeModule, declarations: [RelativeTimePipe], exports: [RelativeTimePipe] });
RelativeTimeModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RelativeTimeModule });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RelativeTimeModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        RelativeTimePipe
                    ],
                    exports: [
                        RelativeTimePipe
                    ]
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { RelativeTimeModule, RelativeTimePipe };
//# sourceMappingURL=ng-devui-relative-time.mjs.map
