"use strict";
/*
 * Copyright (c) 2021-2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseQueryRelations = void 0;
const minimatch_1 = __importDefault(require("minimatch"));
const utils_1 = require("../../utils");
const utils_2 = require("../utils");
const utils_3 = require("./utils");
// --------------------------------------------------
function parseQueryRelations(data, options) {
    var _a;
    options !== null && options !== void 0 ? options : (options = {});
    // If it is an empty array nothing is allowed
    if (Array.isArray(options.allowed) &&
        options.allowed.length === 0) {
        return [];
    }
    options.mapping = options.mapping || {};
    options.pathMapping = options.pathMapping || {};
    (_a = options.includeParents) !== null && _a !== void 0 ? _a : (options.includeParents = true);
    let items = [];
    const prototype = Object.prototype.toString.call(data);
    if (prototype !== '[object Array]' &&
        prototype !== '[object String]') {
        return [];
    }
    if (prototype === '[object String]') {
        items = data.split(',');
    }
    if (prototype === '[object Array]') {
        items = data.filter((el) => typeof el === 'string');
    }
    if (items.length === 0) {
        return [];
    }
    const mappingKeys = Object.keys(options.mapping);
    if (mappingKeys.length > 0) {
        for (let i = 0; i < items.length; i++) {
            items[i] = (0, utils_1.applyMapping)(items[i], options.mapping);
        }
    }
    if (options.allowed) {
        items = items.filter((item) => (0, utils_2.isPathCoveredByParseAllowedOption)(options.allowed, item));
    }
    else {
        items = items.filter((item) => (0, utils_3.isValidRelationPath)(item));
    }
    if (options.includeParents) {
        if (Array.isArray(options.includeParents)) {
            const parentIncludes = items.filter((item) => item.includes('.') &&
                options.includeParents.filter((parent) => (0, minimatch_1.default)(item, parent)).length > 0);
            items.unshift(...(0, utils_3.includeParents)(parentIncludes));
        }
        else {
            items = (0, utils_3.includeParents)(items);
        }
    }
    items = Array.from(new Set(items));
    return items
        .map((key) => {
        const parts = key.split('.');
        let value;
        if (options.pathMapping &&
            (0, utils_1.hasOwnProperty)(options.pathMapping, key)) {
            value = options.pathMapping[key];
        }
        else {
            value = parts.pop();
        }
        return {
            key,
            value,
        };
    });
}
exports.parseQueryRelations = parseQueryRelations;
//# sourceMappingURL=parse.js.map