(function (window, document) {
    'use strict';

    function readCalendar() {
        var clock = typeof window.zertiAdminClock === 'object' && window.zertiAdminClock ? window.zertiAdminClock : null;
        if (clock && clock.calendar) {
            return clock.calendar;
        }
        var cfg = typeof window.zertiComponents === 'object' && window.zertiComponents ? window.zertiComponents : null;
        if (cfg && cfg.calendar) {
            return cfg.calendar;
        }
        return 'jalali';
    }

    function initDatepickers(root) {
        if (typeof window.zertiResetAdminDateInputs === 'function') {
            window.zertiResetAdminDateInputs(root);
        }
        if (readCalendar() === 'gregorian') {
            if (typeof window.zertiInitGregorianDatepickers === 'function') {
                window.zertiInitGregorianDatepickers(root);
            }
            return;
        }
        if (readCalendar() === 'hijri') {
            if (typeof window.zertiInitHijriDatepickers === 'function') {
                window.zertiInitHijriDatepickers(root);
            }
            return;
        }
        if (typeof window.zertiInitJalaliDatepickers === 'function') {
            window.zertiInitJalaliDatepickers(root);
        }
    }

    window.zertiInitDatepickers = initDatepickers;

    function boot() {
        initDatepickers(document);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', boot);
    } else {
        boot();
    }
})(window, document);
