export class RelativeDateFormatter {
    constructor(locale) {
        this.locale = locale;
    }
    format(value, format) {
        const formatter = new Intl.RelativeTimeFormat(this.locale, format.options);
        const { value: relativeTime, unit } = this.getRelativeTime(value);
        const parts = formatter.formatToParts(relativeTime, unit);
        let prefix = '';
        let formattedValue = '';
        let suffix = '';
        for (const part of parts) {
            if (part.type === 'literal' && !formattedValue) {
                prefix += part.value;
            }
            else if (part.type === 'literal' && formattedValue) {
                suffix += part.value;
            }
            else {
                formattedValue += part.value;
            }
        }
        if (!formattedValue) {
            formattedValue = prefix || suffix;
            prefix = '';
            suffix = '';
        }
        return {
            prefix: prefix,
            value: formattedValue,
            suffix: suffix,
        };
    }
    getRelativeTime(value) {
        const date = new Date(value);
        const now = new Date();
        const diffInMs = date.getTime() - now.getTime();
        const times = [
            { unit: 'year', value: 1000 * 60 * 60 * 24 * 365 },
            { unit: 'month', value: 1000 * 60 * 60 * 24 * 30 },
            { unit: 'week', value: 1000 * 60 * 60 * 24 * 7 },
            { unit: 'day', value: 1000 * 60 * 60 * 24 },
            { unit: 'hour', value: 1000 * 60 * 60 },
            { unit: 'minute', value: 1000 * 60 },
            { unit: 'second', value: 1000 },
        ];
        for (const time of times) {
            const diff = diffInMs / time.value;
            if (Math.abs(diff) >= 1) {
                return { value: Math.round(diff), unit: time.unit };
            }
        }
        return { value: 0, unit: 'second' };
    }
}
