"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findMatchingModelFromConfig = exports.findMatchingModel = void 0;
var models_1 = require("./models");
var matchWithDot = function (model, key, value) {
    return typeof value === 'string' && value.split('.').pop() === model[key];
};
var matchPath = function (model, key, value) {
    return key === 'path' && typeof value === 'string' && model[key].startsWith(value);
};
var findMatchingModel = function (model, config) {
    if (config === void 0) { config = {
        useDotValues: false
    }; }
    return Object.keys(model).length > 0
        ? models_1.models.links.find(function (m) {
            return Object.entries(model).every(function (_a) {
                var _b = __read(_a, 2), key = _b[0], value = _b[1];
                return value === m[key] ||
                    (config.useDotValues && matchWithDot(m, key, value)) ||
                    matchPath(m, key, value);
            });
        })
        : null;
};
exports.findMatchingModel = findMatchingModel;
var modelMatchOrder = function (_a) {
    var model = _a.model, value = _a.value;
    return [
        { model: model, term: value },
        { model: model, modelKey: value },
        {
            model: model,
            modelKey: value.split('.').pop()
        },
        {
            model: model,
            term: value.split('.').pop()
        },
        {
            model: model,
            path: ['hestia_earth', 'models', model, value].join('/')
        },
        !value || value === 'all' ? { model: model } : null
    ].filter(Boolean);
};
var findMatchingModelFromConfig = function (model) {
    var _a, _b;
    return (_b = (_a = modelMatchOrder(model)
        .map(function (params) {
        return (0, exports.findMatchingModel)(params) ||
            (0, exports.findMatchingModel)(params, { useDotValues: true });
    })) === null || _a === void 0 ? void 0 : _a.filter(Boolean)) === null || _b === void 0 ? void 0 : _b.shift();
};
exports.findMatchingModelFromConfig = findMatchingModelFromConfig;
