"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityDefinitionMapper = void 0;
const ta_json_1 = require("ta-json");
const base_1 = require("../contracts/base");
const guard_1 = __importDefault(require("../guard"));
const entity_definition_resource_1 = require("../models/entity-definition-resource");
const member_group_mapper_1 = require("./member-group-mapper");
class EntityDefinitionMapper {
    constructor(client) {
        guard_1.default.notNullOrUndefined(client, "client");
        this._client = client;
        this._memberGroupMapper = new member_group_mapper_1.MemberGroupMapper(client);
    }
    //#region To SDK
    mapEntityDefinitionsAsync(resources) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!resources)
                return [];
            if (!(resources instanceof Array)) {
                resources = resources.items;
            }
            if (resources.length === 0)
                return [];
            const list = [];
            for (const resource of resources) {
                const entityDefinition = yield this.mapEntityDefinitionAsync(resource);
                if (entityDefinition != null) {
                    list.push(entityDefinition);
                }
            }
            return list;
        });
    }
    mapEntityDefinitionAsync(resource) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!resource)
                return null;
            const _a = ta_json_1.TaJson.deserialize(ta_json_1.TaJson.serialize(resource), entity_definition_resource_1.EntityDefinitionResource), { 
            // eslint-disable-next-line @typescript-eslint/no-unused-vars
            entities, 
            // eslint-disable-next-line @typescript-eslint/no-unused-vars
            relatedPaths, createdBy, modifiedBy, permissions, memberGroups, labels } = _a, definitionData = __rest(_a, ["entities", "relatedPaths", "createdBy", "modifiedBy", "permissions", "memberGroups", "labels"]);
            const definition = new base_1.EntityDefinition(Object.assign(Object.assign({}, definitionData), { createdBy: (yield this._client.linkHelper.idFromEntityAsync(createdBy)) || undefined, modifiedBy: (yield this._client.linkHelper.idFromEntityAsync(modifiedBy)) || undefined, memberGroups: yield this._memberGroupMapper.mapMemberGroupsAsync(memberGroups), labels: JSON.parse(JSON.stringify(labels)), permissions }));
            return definition;
        });
    }
    //#endregion
    //#region To API resource
    mapEntityDefinitionResourceAsync(definition) {
        return __awaiter(this, void 0, void 0, function* () {
            if (definition == null)
                return null;
            const memberGroups = yield this._memberGroupMapper.mapMemberGroupResourcesAsync(definition.memberGroups);
            return new entity_definition_resource_1.EntityDefinitionResource({
                name: definition.name,
                id: definition.id || 0,
                displayTemplate: definition.displayTemplate,
                isManualSortingAllowed: definition.isManualSortingAllowed,
                isSystemOwned: definition.isSystemOwned,
                isTaxonomyItemDefinition: definition.isTaxonomyItemDefinition,
                isPathEnabledDefinition: definition.isPathEnabledDefinition,
                memberGroups: memberGroups,
                labels: JSON.parse(JSON.stringify(definition.labels)),
            });
        });
    }
}
exports.EntityDefinitionMapper = EntityDefinitionMapper;
//# sourceMappingURL=entity-definition-mapper.js.map