'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib_es6 = require('./tslib.es6-2c2ed574.js');
var normalizeLink = require('./normalizeLink-20e290e8.js');
var core = require('./core-d07a5649.js');
require('./constantsDomainLocales.js');
require('./Locale-fbe01177.js');
require('./constantsIsProduction.js');

const formatHealthTool = (healthTool, locale) => {
    var _a, _b;
    if (!healthTool)
        return {};
    const prefix = locale === 'tl-PH' ? '/fil' : '';
    return {
        id: healthTool.id,
        isSubot: healthTool.is_subot,
        template: healthTool.template,
        icon: healthTool.icon,
        link: ((_a = healthTool.link) === null || _a === void 0 ? void 0 : _a.startsWith('http'))
            ? healthTool.link
            : `${prefix}/health-tools` +
                ((_b = healthTool.link) === null || _b === void 0 ? void 0 : _b.replace(`/${prefix}/`, '/').replace(`/health-tools/`, '/')),
        subtitle: healthTool.subtitle,
        title: healthTool.title,
    };
};
const getDynamicHealthTool = (locale) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    try {
        const customLang = locale === 'tl-PH' ? 'lang=fil' : '';
        let response = [];
        const isMarryBaby = locale.endsWith('_MB');
        try {
            const healthTools = yield fetch(normalizeLink.normalizeLinkSlash(normalizeLink.normalizeLinkSlash(`${normalizeLink.getDiscoverApiDomain(locale)}/wp-json/api/${isMarryBaby ? 'health-tools' : 'healthtools'}?ismenu=0${customLang ? '&' + customLang : ''}`))).then((res) => res.json());
            response =
                ((_b = (_a = healthTools === null || healthTools === void 0 ? void 0 : healthTools.data) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, (healthTool) => formatHealthTool(healthTool, locale))) || [];
        }
        catch (_c) { }
        return response;
    }
    catch (e) {
        console.error(e);
    }
});
const getDynamicHealthToolWithCache = (locale) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
    return (yield core.getDataWithCache('all-health-tools', () => getDynamicHealthTool(locale || 'en-PH'), (res) => !!(res === null || res === void 0 ? void 0 : res.length), 1000 * 60)) || [];
});

exports.formatHealthTool = formatHealthTool;
exports.getDynamicHealthTool = getDynamicHealthTool;
exports.getDynamicHealthToolWithCache = getDynamicHealthToolWithCache;
