import { store } from 'src/app/redux/store';
import { minuteInMs } from 'src/lib/constants';
import { getTimeObj } from 'src/lib/utils';
export var getFiltersCacheId = function (filters) {
    var patientType = filters.patientType.charAt(0);
    var isOrthoDoc = filters.isOrthoDoc, isGeneralDoc = filters.isGeneralDoc, isPediatric = filters.isPediatric;
    var doctorType = getDoctorTypeForCache({ isOrthoDoc: isOrthoDoc, isGeneralDoc: isGeneralDoc, isPediatric: isPediatric });
    var zip = filters.zip;
    var radius = filters.radius;
    var tier = filters.tier;
    var toggleFilter = store.getState().config.config.mainToggleFilter;
    var address = filters.address.replace(/\s/g, '');
    var city = filters.city.replace(/\s/g, '');
    var state = filters.state;
    var docName = filters.doctorName.replace(/\s/g, '');
    var sortType = filters.sortType;
    var partner = (filters === null || filters === void 0 ? void 0 : filters.partner) || '';
    var program = (filters === null || filters === void 0 ? void 0 : filters.program) || '';
    var fehours = filters.fehours;
    var fservice = filters.fservice.filter(Boolean).toString();
    var flan = filters.flan.toString();
    var treatments = JSON.stringify(filters.treatments);
    var fpinsurance = filters.fpinsurance.toString();
    var lat = filters.lat;
    var lng = filters.lng;
    var cacheId = "".concat(patientType).concat(doctorType).concat(zip).concat(radius).concat(tier).concat(fehours).concat(fpinsurance).concat(flan).concat(fservice).concat(treatments).concat(toggleFilter.charAt(0)).concat(filters[toggleFilter] ? 'y' : 'n').concat(address).concat(city).concat(state).concat(docName).concat(sortType).concat(partner).concat(program).concat(lat).concat(lng);
    return cacheId;
};
export var getCachesFromEntries = function (entries) {
    var caches = {};
    entries.forEach(function (entry) { return (caches[entry[0]] = entry[1]); });
    return caches;
};
export var sortCacheByTime = function (cacheEntries) {
    return cacheEntries.sort(function (entryA, entryB) {
        var createdAtA = entryA[1].createdAt;
        var createdAtB = entryB[1].createdAt;
        var _a = getTimeObj(createdAtA), minsA = _a.minutes, secsA = _a.seconds, msA = _a.ms;
        var _b = getTimeObj(createdAtB), minsB = _b.minutes, secsB = _b.seconds, msB = _b.ms;
        if (minsA > minsB) {
            return 1;
        }
        else if (minsA < minsB) {
            return 0;
        }
        else if (secsA > secsB) {
            return 1;
        }
        else if (secsA < secsB) {
            return 0;
        }
        else
            return msA - msB;
    });
};
export var isCacheExpired = function (cache) {
    var cacheCreatedAt = getTimeObj(cache.createdAt);
    var hours = cacheCreatedAt.hours, minutes = cacheCreatedAt.minutes;
    var cacheTime = new Date();
    cacheTime.setHours(hours);
    cacheTime.setMinutes(minutes);
    return ((new Date().getTime() - cacheTime.getTime()) / minuteInMs >=
        process.env.REACT_APP_CACHE_HOLD_TIME_IN_MINS);
};
export var getDoctorTypeForCache = function (_a) {
    var isOrthoDoc = _a.isOrthoDoc, isGeneralDoc = _a.isGeneralDoc, isPediatric = _a.isPediatric;
    if (!isGeneralDoc && isOrthoDoc && !isPediatric)
        return 'fo';
    if (isGeneralDoc && !isOrthoDoc && !isPediatric)
        return 'fgp';
    if (!isGeneralDoc && !isOrthoDoc && isPediatric)
        return 'fpeds';
    if (isGeneralDoc && isOrthoDoc && !isPediatric)
        return 'ogp';
    if (!isGeneralDoc && isOrthoDoc && isPediatric)
        return 'opeds';
    if (isGeneralDoc && !isOrthoDoc && isPediatric)
        return 'gpeds';
    return "fa";
};
