"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractIdentity = void 0;
const Permissions_1 = require("@dfns/datamodel/dist/Permissions");
const types_1 = require("../types");
const utils_1 = require("../utils");
const AUTH_KIND_TO_IDENTITY_KIND = {
    [types_1.UserAuthKind.EndUser]: Permissions_1.IdentityKind.EndUser,
    [types_1.UserAuthKind.Employee]: Permissions_1.IdentityKind.CustomerEmployee,
    [types_1.UserAuthKind.Staff]: Permissions_1.IdentityKind.DfnsStaff,
};
const extractIdentity = (authDetails) => {
    if (authDetails.authBlock) {
        // V2
        const auth = authDetails.authBlock.auth;
        if (auth.user) {
            const identityKind = AUTH_KIND_TO_IDENTITY_KIND[auth.user.identity.kind];
            if (!identityKind)
                throw Error(`Unsupported UserAuthKind ${auth.user.identity.kind}`);
            return { identityId: auth.user.identity.id, identityKind };
        }
        else {
            return { identityId: auth.app.identity.id, identityKind: Permissions_1.IdentityKind.Application };
        }
    }
    else {
        // V1
        if ((0, utils_1.isOrgEmployeeIdentity)(authDetails.identity)) {
            return { identityId: authDetails.identity.employeeId, identityKind: Permissions_1.IdentityKind.CustomerEmployee };
        }
        else if ((0, utils_1.isOrgApiKeyIdentity)(authDetails.identity)) {
            return { identityId: authDetails.identity.apiKeyId, identityKind: Permissions_1.IdentityKind.CustomerEmployee };
        }
        else
            throw Error('Unsupported identity kind');
    }
};
exports.extractIdentity = extractIdentity;
//# sourceMappingURL=permissions.js.map