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());
    });
};
import { Rule } from "../contracts/policies/rule";
import { RuleResource } from "../models/policies/rule-resource";
import { ConditionMapper } from "./condition-mapper";
import { RuleTypeMapper } from "./rule-type-mapper";
export class RulesMapper {
    /**
     * Map rule resources to rules.
     * @param client - An instance of an extended Content Hub client
     * @param ruleResources - A collection of resources to map
     * @returns An array of rules.
     */
    static mapToRulesAsync(client, ruleResources) {
        return __awaiter(this, void 0, void 0, function* () {
            if (ruleResources == null || ruleResources.length === 0) {
                return [];
            }
            const list = [];
            for (const ruleResource of ruleResources) {
                const rule = yield this.mapToRuleAsync(client, ruleResource);
                if (rule != null) {
                    list.push(rule);
                }
            }
            return list;
        });
    }
    /**
     * Map a rule resource to a rule instance.
     * @param client - An instance of an extended Content Hub client
     * @param ruleResource - A rule resource
     *
     * @returns A rule or null.
     */
    static mapToRuleAsync(client, ruleResource) {
        return __awaiter(this, void 0, void 0, function* () {
            if (ruleResource == null) {
                return null;
            }
            const conditions = yield ConditionMapper.mapToConditionsAsync(client, ruleResource.conditions);
            //TODO Decide if we need a Guid type
            const guid = ruleResource.identifier;
            const type = RuleTypeMapper.mapToRuleType(ruleResource.type);
            const definitionNames = yield client.linkHelper.namesFromDefinitionsAsync(ruleResource.definitions);
            const sdkRule = new Rule({
                conditions: conditions,
                createdByLoggedOnUser: ruleResource.createdByLoggedOnUser,
                definitionNames: definitionNames.filter(name => name != null),
                isSystemOwned: ruleResource.isSystemOwned,
                permissions: ruleResource.permissions,
                type: type,
            });
            sdkRule.setIdentifier(guid);
            return sdkRule;
        });
    }
    /**
     * Map rule instances to rule resources.
     * @param client - An instance of an extended Content Hub client
     * @param rules - A collection of rules
     * @returns An array of rule resources.
     */
    static mapToRuleResourcesAsync(client, rules) {
        return __awaiter(this, void 0, void 0, function* () {
            if (rules == null || rules.length === 0) {
                return [];
            }
            const list = [];
            for (const rule of rules) {
                const ruleResource = yield this.mapToRuleResourceAsync(client, rule);
                if (ruleResource != null) {
                    list.push(ruleResource);
                }
            }
            return list;
        });
    }
    /**
     * Map a rule to a rule resource.
     * @param client - An instance of an extended Content Hub client
     * @param rule - A rule instance
     * @returns A rule resource or null.
     */
    static mapToRuleResourceAsync(client, rule) {
        return __awaiter(this, void 0, void 0, function* () {
            if (rule == null) {
                return null;
            }
            const conditions = yield ConditionMapper.mapToConditionResourcesAsync(client, rule.conditions);
            const identifier = rule.identifier;
            const permissions = rule.permissions;
            const type = RuleTypeMapper.mapToRuleResourceType(rule.type);
            const definitionLinkTasks = [];
            for (const definitionName of rule.definitionNames) {
                const definitionLink = client.linkHelper.definitionToLinkAsync(definitionName);
                definitionLinkTasks.push(definitionLink);
            }
            const definitionLinks = yield Promise.all(definitionLinkTasks);
            const ruleResource = new RuleResource({
                conditions: conditions,
                createdByLoggedOnUser: rule.createdByLoggedOnUser,
                definitions: definitionLinks,
                identifier: identifier,
                isSystemOwned: rule.isSystemOwned,
                permissions: permissions,
                type: type,
            });
            return ruleResource;
        });
    }
}
//# sourceMappingURL=rules-mapper.js.map