/* https://wiki.aligntech.com/display/CON/Doclocator+Search+API */
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { store } from 'src/app/redux/store';
import { kmInMiles } from 'src/lib/constants';
import { PatientType, } from 'src/lib/types/types';
import { postalCodeBMRegex, postalCodeCARegex, postalCodePRRegex, postalCodeUSRegex, postalCodeVIRegex, } from 'src/lib/utils';
export var getApiFilters = function (filters) {
    var _a, _b;
    var _c = store.getState().config.config, localeId = _c.localeId, country = _c.country, countryData = _c.countryData, apiCountryList = _c.apiCountryList, implementationType = _c.implementationType, hideFilters = _c.hideFilters, isLatam = _c.isLatam, useRecaptchaForAPIs = _c.useRecaptchaForAPIs, servicesOfferedFilterData = _c.servicesOfferedFilterData;
    var partner = store.getState().common.partner;
    var useRecaptcha = useRecaptchaForAPIs === true;
    var defaultRdi = String((_a = countryData[country]) === null || _a === void 0 ? void 0 : _a.defaultRadiusIncrement);
    var configCountryList = (_b = countryData[country]) === null || _b === void 0 ? void 0 : _b.countryList.toLocaleUpperCase();
    var countryList = getCountryList(filters.zip) ||
        (filters.latitude && filters.longitude && country.toUpperCase()) ||
        configCountryList;
    var token = filters.token || '';
    var _d = (partner === null || partner === void 0 ? void 0 : partner.searchFilters) || {}, f = _d.f, rd = _d.rd, rest = __rest(_d, ["f", "rd"]);
    var checker = filters.fservice.filter(Boolean).join(',');
    var hasOls = checker.includes('ols') || checker.includes('Online scheduling');
    var filteredServices = servicesOfferedFilterData === null || servicesOfferedFilterData === void 0 ? void 0 : servicesOfferedFilterData.filter(function (item) {
        if (item.key === 'ols')
            return false;
        return checker.includes(item.key) || checker.includes(item.value);
    }).map(function (item) { return item.value; }).join(',');
    var apiFilters = __assign(__assign(__assign({ token: useRecaptcha ? token : '', av: filters.apiVersion || 'nl2', cid: !filters.longitude || !filters.latitude ? 'datacore' : '', cl: countryList || 'US', f: getFilterParam(filters, f), fdp: getProgramFilter(filters.program), fn: filters.doctorName, it: implementationType, lat: filters.latitude || '', lng: filters.longitude || '', pc: filters.zip.replace(/_/g, ''), pst: getPresetParam(filters.radius, isLatam), rd: getRadiusFilter(filters, localeId, rd || ''), rdi: defaultRdi || '2.5', s: filters.sortType, sid: filters.sessionId, uct: filters.city, usa: filters.address, ust: (hideFilters === null || hideFilters === void 0 ? void 0 : hideFilters.state) ? '' : filters.state, vid: filters.visitorId, flan: filters.flan.join(','), fpinsurance: filters.fpinsurance.join(','), fservice: filteredServices }, (hasOls && { svc: 'ols' })), { mtcc: '1', mrd: isLatam ? '150' : filters.patientType === PatientType.ADULT ? '50' : '25' }), rest);
    //@ts-ignore
    if (filters.fehours === true) {
        apiFilters.fehours = 'true';
    }
    if (!useRecaptcha)
        delete apiFilters['token'];
    for (var key in apiFilters) {
        var value = apiFilters[key];
        if (!value || value === 'undefined')
            delete apiFilters[key];
    }
    return apiFilters;
};
export var getPresetParam = function (radius, isLatam) {
    return radius ? '2' : isLatam ? '1' : '3';
};
export var getProgramFilter = function (program) {
    var programData = store.getState().config.config.programData;
    var programFilter = programData === null || programData === void 0 ? void 0 : programData.find(function (item) { return item.filterValue === program; });
    return programFilter ? programFilter.programKey : '';
};
export var getFilterParam = function (filters, customFilter) {
    var _a = store.getState().config.config, mainToggleFilter = _a.mainToggleFilter, hideFilters = _a.hideFilters, showFilters = _a.showFilters, country = _a.country;
    var mainToggle = store.getState().filter.filters[mainToggleFilter];
    var tier = filters.tier, isOrthoDoc = filters.isOrthoDoc, isGeneralDoc = filters.isGeneralDoc, isPediatric = filters.isPediatric, patientType = filters.patientType, program = filters.program, treatments = filters.treatments;
    var filter = 'F1';
    var tierPins = store.getState().config.config.tierPins;
    var tierData = tierPins.find(function (pins) { return pins.segmentCode === tier; });
    if (tier && (tierData === null || tierData === void 0 ? void 0 : tierData.filterValue))
        filter += ",".concat(tierData === null || tierData === void 0 ? void 0 : tierData.filterValue);
    if (tier && isNaN(tier))
        filter += ",".concat(tier);
    if (!(hideFilters === null || hideFilters === void 0 ? void 0 : hideFilters.doctorType)) {
        var isPediatricDoc = (showFilters === null || showFilters === void 0 ? void 0 : showFilters.isPediatric) && isPediatric;
        if (!isGeneralDoc && isOrthoDoc && !isPediatricDoc)
            filter += ',fo';
        if (isGeneralDoc && !isOrthoDoc && !isPediatricDoc)
            filter += ',fgp';
        if (!isGeneralDoc && !isOrthoDoc && isPediatricDoc)
            filter += ',fpeds';
        if (isGeneralDoc && isOrthoDoc && !isPediatricDoc)
            filter += ',ogp';
        if (!isGeneralDoc && isOrthoDoc && isPediatricDoc)
            filter += ',opeds';
        if (isGeneralDoc && !isOrthoDoc && isPediatricDoc)
            filter += ',gpeds';
    }
    if (patientType === PatientType.TEEN)
        filter += ',ft';
    if (mainToggle && (!hideFilters || !hideFilters[mainToggleFilter]))
        filter += ",".concat(mainToggleFilter.toUpperCase());
    if (customFilter)
        filter += ",".concat(customFilter);
    if (treatments === null || treatments === void 0 ? void 0 : treatments.itero) {
        filter += ",itero";
    }
    if (treatments === null || treatments === void 0 ? void 0 : treatments.whitening) {
        filter += ",WHITENING";
    }
    if (treatments === null || treatments === void 0 ? void 0 : treatments.vivera_retainers) {
        filter += ",Vivera";
    }
    if (treatments === null || treatments === void 0 ? void 0 : treatments.ipe) {
        filter += ",ipe";
    }
    if (treatments === null || treatments === void 0 ? void 0 : treatments.invisalign_first) {
        filter += ",IVF";
    }
    if (treatments === null || treatments === void 0 ? void 0 : treatments.aohs) {
        filter += ",aohs";
    }
    return filter;
};
export var getRadiusFilter = function (filters, localeId, customRadius) {
    var radius = filters.radius;
    var _a = store.getState().config.config, isLatam = _a.isLatam, zipsFor12MilesRadius = _a.zipsFor12MilesRadius, zipsFor2point5MilesRadius = _a.zipsFor2point5MilesRadius;
    var isCA = (localeId === null || localeId === void 0 ? void 0 : localeId.split('-')[1]) === 'CA';
    if (radius) {
        if (isNaN(Number(radius)))
            return '0';
        if (isCA)
            return String(Number(radius) * kmInMiles);
        return radius;
    }
    else if (customRadius)
        return isNaN(Number(customRadius)) ? '0' : customRadius;
    if (isCA || isLatam)
        return '0';
    var patientType = filters.patientType, zip = filters.zip;
    return String(patientType === PatientType.TEEN
        ? 10
        : zipsFor12MilesRadius.map(String).indexOf(zip) > -1
            ? 12
            : zipsFor2point5MilesRadius.map(String).indexOf(zip) > -1
                ? 2.5
                : 7.5);
};
export var getCountryList = function (zip) {
    if (zip.match(postalCodeVIRegex))
        return 'US,VI';
    if (zip.match(postalCodePRRegex))
        return 'US,PR';
    if (zip.match(postalCodeUSRegex))
        return 'US';
    if (zip.match(postalCodeCARegex))
        return 'CA';
    if (zip.match(postalCodeBMRegex))
        return 'BM';
    return '';
};
export var getCountryListForGeocode = function (zip, asFullName) {
    if (zip.match(postalCodeVIRegex))
        return asFullName ? 'Virgin Islands' : 'USA,VI';
    if (zip.match(postalCodePRRegex))
        return asFullName ? 'Puerto Rico' : 'USA,PR';
    if (zip.match(postalCodeUSRegex))
        return 'USA';
    if (zip.match(postalCodeCARegex))
        return 'CA';
    if (zip.match(postalCodeBMRegex))
        return 'BM';
    return '';
};
