// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { getIdentifierKind, getIdentifierRawId, } from "./identifierModels";
const assertNotNullOrUndefined = (obj, prop) => {
    const subObjName = Object.keys(obj)[0];
    const subObj = obj[subObjName];
    if (prop in subObj) {
        return subObj[prop];
    }
    throw new Error(`Property ${prop} is required for identifier of type ${subObjName}.`);
};
const assertMaximumOneNestedModel = (identifier) => {
    const presentProperties = [];
    if (identifier.communicationUser !== undefined) {
        presentProperties.push("communicationUser");
    }
    if (identifier.microsoftTeamsUser !== undefined) {
        presentProperties.push("microsoftTeamsUser");
    }
    if (identifier.microsoftTeamsApp !== undefined) {
        presentProperties.push("microsoftTeamsApp");
    }
    if (identifier.phoneNumber !== undefined) {
        presentProperties.push("phoneNumber");
    }
    if (presentProperties.length > 1) {
        throw new Error(`Only one of the properties in ${JSON.stringify(presentProperties)} should be present.`);
    }
};
/**
 * @hidden
 * Translates a CommunicationIdentifier to its serialized format for sending a request.
 * @param identifier - The CommunicationIdentifier to be serialized.
 */
export const serializeCommunicationIdentifier = (identifier) => {
    var _a, _b, _c, _d, _e, _f;
    const identifierKind = getIdentifierKind(identifier);
    switch (identifierKind.kind) {
        case "communicationUser":
            return {
                rawId: getIdentifierRawId(identifierKind),
                communicationUser: { id: identifierKind.communicationUserId },
            };
        case "phoneNumber":
            return {
                rawId: (_a = identifierKind.rawId) !== null && _a !== void 0 ? _a : getIdentifierRawId(identifierKind),
                phoneNumber: {
                    value: identifierKind.phoneNumber,
                },
            };
        case "microsoftTeamsUser":
            return {
                rawId: (_b = identifierKind.rawId) !== null && _b !== void 0 ? _b : getIdentifierRawId(identifierKind),
                microsoftTeamsUser: {
                    userId: identifierKind.microsoftTeamsUserId,
                    isAnonymous: (_c = identifierKind.isAnonymous) !== null && _c !== void 0 ? _c : false,
                    cloud: (_d = identifierKind.cloud) !== null && _d !== void 0 ? _d : "public",
                },
            };
        case "microsoftTeamsApp":
            return {
                rawId: (_e = identifierKind.rawId) !== null && _e !== void 0 ? _e : getIdentifierRawId(identifierKind),
                microsoftTeamsApp: {
                    appId: identifierKind.teamsAppId,
                    cloud: (_f = identifierKind.cloud) !== null && _f !== void 0 ? _f : "public",
                },
            };
        case "unknown":
            return { rawId: identifierKind.id };
        default:
            throw new Error(`Can't serialize an identifier with kind ${identifierKind.kind}`);
    }
};
const getKind = (serializedIdentifier) => {
    if (serializedIdentifier.communicationUser) {
        return "communicationUser";
    }
    if (serializedIdentifier.phoneNumber) {
        return "phoneNumber";
    }
    if (serializedIdentifier.microsoftTeamsUser) {
        return "microsoftTeamsUser";
    }
    if (serializedIdentifier.microsoftTeamsApp) {
        return "microsoftTeamsApp";
    }
    return "unknown";
};
/**
 * @hidden
 * Translates the serialized format of a communication identifier to CommunicationIdentifier.
 * @param serializedIdentifier - The SerializedCommunicationIdentifier to be deserialized.
 */
export const deserializeCommunicationIdentifier = (serializedIdentifier) => {
    var _a;
    assertMaximumOneNestedModel(serializedIdentifier);
    const { communicationUser, microsoftTeamsUser, microsoftTeamsApp, phoneNumber } = serializedIdentifier;
    const kind = (_a = serializedIdentifier.kind) !== null && _a !== void 0 ? _a : getKind(serializedIdentifier);
    if (kind === "communicationUser" && communicationUser) {
        return {
            kind: "communicationUser",
            communicationUserId: assertNotNullOrUndefined({ communicationUser }, "id"),
        };
    }
    if (kind === "phoneNumber" && phoneNumber) {
        return {
            kind: "phoneNumber",
            phoneNumber: assertNotNullOrUndefined({ phoneNumber }, "value"),
            rawId: assertNotNullOrUndefined({ phoneNumber: serializedIdentifier }, "rawId"),
        };
    }
    if (kind === "microsoftTeamsUser" && microsoftTeamsUser) {
        return {
            kind: "microsoftTeamsUser",
            microsoftTeamsUserId: assertNotNullOrUndefined({ microsoftTeamsUser }, "userId"),
            isAnonymous: assertNotNullOrUndefined({ microsoftTeamsUser }, "isAnonymous"),
            cloud: assertNotNullOrUndefined({ microsoftTeamsUser }, "cloud"),
            rawId: assertNotNullOrUndefined({ microsoftTeamsUser: serializedIdentifier }, "rawId"),
        };
    }
    if (kind === "microsoftTeamsApp" && microsoftTeamsApp) {
        return {
            kind: "microsoftTeamsApp",
            teamsAppId: assertNotNullOrUndefined({ microsoftTeamsApp }, "appId"),
            cloud: assertNotNullOrUndefined({ microsoftTeamsApp }, "cloud"),
            rawId: assertNotNullOrUndefined({ microsoftTeamsApp: serializedIdentifier }, "rawId"),
        };
    }
    return {
        kind: "unknown",
        id: assertNotNullOrUndefined({ unknown: serializedIdentifier }, "rawId"),
    };
};
//# sourceMappingURL=identifierModelSerializer.js.map