import { defineComponent, toRefs, inject, getCurrentInstance, computed, isRef, ref, watch, h } from 'vue';
import { formatDistanceToNow } from 'date-fns';

/*
 * Copyright (c) 2023.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */ const InjectionKey = '$timeago';

const convert = (date, locale, converterOptions)=>{
    const { includeSeconds, addSuffix = true } = converterOptions || {};
    return formatDistanceToNow(date, {
        locale,
        includeSeconds,
        addSuffix
    });
};

const Timeago = defineComponent({
    props: {
        datetime: {
            type: [
                Object,
                Number,
                String
            ],
            required: true
        },
        title: {
            type: [
                String,
                Boolean
            ]
        },
        locale: {
            type: String
        },
        autoUpdate: {
            type: [
                Number,
                Boolean
            ]
        },
        converter: {
            type: Function
        },
        converterOptions: {
            type: Object
        }
    },
    setup (props, ctx) {
        const refs = toRefs(props);
        const context = inject(InjectionKey);
        const getContextProperty = (key)=>{
            if (!context) {
                return undefined;
            }
            return context[key];
        };
        const locales = getContextProperty('locales') || {};
        const instance = getCurrentInstance();
        const instanceLocale = computed(()=>{
            if (!instance) {
                return 'en';
            }
            const value = instance.appContext.config.globalProperties.$timeagoLocale;
            if (!value) {
                return 'en';
            }
            return isRef(value) ? value.value : value;
        });
        const time = ref('');
        const calculate = (dateTime)=>{
            let converter = refs.converter.value;
            if (!converter && context && context.converter) {
                converter = context.converter;
            }
            if (!converter) {
                converter = convert;
            }
            if (!dateTime) {
                if (typeof refs.datetime.value === 'string') {
                    dateTime = new Date(refs.datetime.value);
                } else {
                    dateTime = refs.datetime.value;
                }
            }
            time.value = converter(dateTime, locales[refs.locale.value || instanceLocale.value], refs.converterOptions.value || {});
        };
        calculate();
        let updater;
        const startUpdater = ()=>{
            if (refs.autoUpdate.value) {
                const autoUpdate = refs.autoUpdate.value === true ? 60 : refs.autoUpdate.value;
                updater = setInterval(()=>{
                    calculate();
                }, autoUpdate * 1000);
            }
        };
        const stopUpdater = ()=>{
            if (updater) {
                clearInterval(updater);
                updater = null;
            }
        };
        watch(refs.autoUpdate, (val)=>{
            stopUpdater();
            if (val) {
                startUpdater();
            }
        });
        watch(refs.datetime, ()=>{
            calculate();
        });
        watch(refs.locale, ()=>{
            calculate();
        });
        watch(refs.converter, ()=>{
            calculate();
        });
        watch(refs.converterOptions, ()=>{
            calculate();
        }, {
            deep: true
        });
        watch(instanceLocale, ()=>{
            calculate();
        });
        return ()=>h('time', {
                attrs: {
                    datetime: new Date(refs.datetime.value).toISOString(),
                    title: typeof refs.title.value === 'string' ? refs.title.value : time.value
                }
            }, [
                time.value
            ]);
    }
});

function install(instance, options) {
    options ?? (options = {});
    instance.provide(InjectionKey, {
        converter: options.converter,
        locales: options.locales || {}
    });
    instance.config.globalProperties.$timeagoLocale = ref(options.locale || 'en');
    instance.component(options.name || 'Timeago', Timeago);
}
var index = {
    install
};

export { Timeago, index as default, install };
//# sourceMappingURL=index.mjs.map
