"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.entityWithDelegationTypeNames = exports.EntityWithDelegationTypeName = void 0;
exports.encryptedEntityClassOf = encryptedEntityClassOf;
exports.tryGetEncryptedEntityClassOf = tryGetEncryptedEntityClassOf;
var EntityWithDelegationTypeName;
(function (EntityWithDelegationTypeName) {
    EntityWithDelegationTypeName["ApplicationSettings"] = "ApplicationSettings";
    EntityWithDelegationTypeName["Article"] = "Article";
    EntityWithDelegationTypeName["AccessLog"] = "AccessLog";
    EntityWithDelegationTypeName["CalendarItem"] = "CalendarItem";
    EntityWithDelegationTypeName["Classification"] = "Classification";
    EntityWithDelegationTypeName["Contact"] = "Contact";
    EntityWithDelegationTypeName["Document"] = "Document";
    EntityWithDelegationTypeName["Form"] = "Form";
    EntityWithDelegationTypeName["HealthElement"] = "HealthElement";
    EntityWithDelegationTypeName["Invoice"] = "Invoice";
    EntityWithDelegationTypeName["MaintenanceTask"] = "MaintenanceTask";
    EntityWithDelegationTypeName["Message"] = "Message";
    EntityWithDelegationTypeName["Patient"] = "Patient";
    EntityWithDelegationTypeName["Receipt"] = "Receipt";
    EntityWithDelegationTypeName["TimeTable"] = "TimeTable";
    EntityWithDelegationTypeName["Topic"] = "Topic";
})(EntityWithDelegationTypeName || (exports.EntityWithDelegationTypeName = EntityWithDelegationTypeName = {}));
exports.entityWithDelegationTypeNames = new Set([
    EntityWithDelegationTypeName.ApplicationSettings,
    EntityWithDelegationTypeName.Article,
    EntityWithDelegationTypeName.AccessLog,
    EntityWithDelegationTypeName.CalendarItem,
    EntityWithDelegationTypeName.Classification,
    EntityWithDelegationTypeName.Contact,
    EntityWithDelegationTypeName.Document,
    EntityWithDelegationTypeName.Form,
    EntityWithDelegationTypeName.HealthElement,
    EntityWithDelegationTypeName.Invoice,
    EntityWithDelegationTypeName.MaintenanceTask,
    EntityWithDelegationTypeName.Message,
    EntityWithDelegationTypeName.Patient,
    EntityWithDelegationTypeName.Receipt,
    EntityWithDelegationTypeName.TimeTable,
]);
/**
 * @internal this function is meant for internal use only and may be changed without notice
 * Gets the class of an encrypted entity. Throws error if the class can't be retrieved.
 * @param entity the entity object
 * @param declaredClassName the declared type of the entity, to use as a fallback in case the original class name can't be retrieved (for example
 * due to the use of minified code).
 * @return the class of the encrypted entity
 */
function encryptedEntityClassOf(entity, declaredClassName) {
    const entityClass = tryGetEncryptedEntityClassOf(entity, declaredClassName);
    if (entityClass) {
        return entityClass;
    }
    else
        throw new Error(`No valid encrypted entity class name (constructor: "${entity.constructor.name}",` +
            `declared: ${declaredClassName ? '"' + declaredClassName + '"' : 'undefined'}).` +
            `Please specify a valid class name.\nValid class names are ${Array.from(exports.entityWithDelegationTypeNames)}.`);
}
/**
 * @internal this function is meant for internal use only and may be changed without notice
 * Gets the class of an encrypted entity. Returns undefined if the class can't be retrieved.
 * @param entity the entity object
 * @param declaredClassName the declared type of the entity, to use as a fallback in case the original class name can't be retrieved (for example
 * due to the use of minified code).
 * @return the class of the encrypted entity
 */
function tryGetEncryptedEntityClassOf(entity, declaredClassName) {
    var _a;
    const _type = entity._type;
    if (_type && (typeof _type === 'string' || _type instanceof String)) {
        const splitType = _type.split('.');
        const candidate = splitType[splitType.length - 1];
        if (exports.entityWithDelegationTypeNames.has(candidate)) {
            if (declaredClassName && declaredClassName !== candidate)
                throw new Error(`Declared type name "${declaredClassName}" does not match type detected from field \`_type:"${_type}"\` ("${candidate}")`);
            return candidate;
        }
    }
    if (!declaredClassName)
        console.warn('Usage of a delegation-related method without specifying the entity class name, and input object does not have a `_type` value. ' +
            'Will use constructor name as fallback, but this may not work with minified code.');
    const constructorName = (_a = entity.constructor) === null || _a === void 0 ? void 0 : _a.name;
    if (exports.entityWithDelegationTypeNames.has(constructorName)) {
        if (declaredClassName && declaredClassName !== constructorName)
            throw new Error(`Declared type name "${declaredClassName}" does not match valid constructor name "${constructorName}"`);
        return constructorName;
    }
    else if (declaredClassName && exports.entityWithDelegationTypeNames.has(declaredClassName)) {
        return declaredClassName;
    }
    else
        return undefined;
}
//# sourceMappingURL=EntityWithDelegationTypeName.js.map