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 { MemberGroup } from "../contracts/base/member-group";
import Guard from "../guard";
import { MemberGroupResource } from "../models/member-group-resource";
import { MemberConditionMapper } from "./member-condition-mapper";
import { MemberDefinitionMapper } from "./member-definition-mapper";
export class MemberGroupMapper {
    constructor(client) {
        Guard.notNullOrUndefined(client, "client");
        this._client = client;
        this._memberDefinitionMapper = new MemberDefinitionMapper(client);
        this._memberConditionMapper = new MemberConditionMapper(client);
    }
    //#region To SDK
    mapMemberGroupsAsync(groupResources) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!groupResources || groupResources.length === 0)
                return [];
            const list = [];
            for (const resource of groupResources) {
                const group = yield this.mapMemberGroupAsync(resource);
                if (group != null) {
                    list.push(group);
                }
            }
            return list;
        });
    }
    mapMemberGroupAsync(groupResource) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!groupResource)
                return null;
            const memberGroup = new MemberGroup({
                name: groupResource.name,
                isSecured: groupResource.isSecured || false,
                isSystemOwned: groupResource.isSystemOwned,
                contentGroup: groupResource.contentGroup,
                conditions: yield this._memberConditionMapper.mapMemberConditionsAsync(groupResource.conditions),
                memberDefinitions: yield this._memberDefinitionMapper.mapMemberDefinitionsAsync(groupResource.memberDefinitions),
                labels: JSON.parse(JSON.stringify(groupResource.labels)),
            });
            return memberGroup;
        });
    }
    //#endregion
    //#region To API resource
    mapMemberGroupResourcesAsync(memberGroups) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!memberGroups || memberGroups.length === 0)
                return [];
            const list = [];
            for (const memberGroup of memberGroups) {
                const resource = yield this.mapMemberGroupResourceAsync(memberGroup);
                if (resource) {
                    list.push(resource);
                }
            }
            return list;
        });
    }
    mapMemberGroupResourceAsync(memberGroup) {
        return __awaiter(this, void 0, void 0, function* () {
            if (memberGroup == null)
                return null;
            const conditions = yield this._memberConditionMapper.mapMemberConditionResourcesAsync(memberGroup.conditions);
            const memberDefinitions = yield this._memberDefinitionMapper.mapMemberDefinitionResourcesAsync(memberGroup.memberDefinitions);
            const resource = new MemberGroupResource({
                name: memberGroup.name,
                isSecured: memberGroup.isSecured != null ? memberGroup.isSecured : false,
                isSystemOwned: memberGroup.isSystemOwned != null ? memberGroup.isSystemOwned : false,
                contentGroup: memberGroup.contentGroup,
                labels: JSON.parse(JSON.stringify(memberGroup.labels || {})),
                conditions: conditions,
                isConditional: memberGroup.isConditional,
                memberDefinitions: memberDefinitions,
            });
            return resource;
        });
    }
}
//# sourceMappingURL=member-group-mapper.js.map