"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Schema
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityReferences = void 0;
const core_common_1 = require("@itwin/core-common");
const core_bentley_1 = require("@itwin/core-bentley");
const Model_1 = require("./Model");
const Element_1 = require("./Element");
const ElementAspect_1 = require("./ElementAspect");
const Relationship_1 = require("./Relationship");
const assert = require("assert");
/**
 * Utilities for the [EntityReference]($common) type which is a kind of strings
 * @alpha
 */
var EntityReferences;
(function (EntityReferences) {
    function isModel(id) {
        return id[0] === core_common_1.ConcreteEntityTypes.Model;
    }
    EntityReferences.isModel = isModel;
    function isElement(id) {
        return id[0] === core_common_1.ConcreteEntityTypes.Element;
    }
    EntityReferences.isElement = isElement;
    function isElementAspect(id) {
        return id[0] === core_common_1.ConcreteEntityTypes.ElementAspect;
    }
    EntityReferences.isElementAspect = isElementAspect;
    function isRelationship(id) {
        return id[0] === core_common_1.ConcreteEntityTypes.Relationship;
    }
    EntityReferences.isRelationship = isRelationship;
    function toId64(id) {
        return id.slice(1);
    }
    EntityReferences.toId64 = toId64;
    /** split a concrete entity id into its type and raw id */
    function split(id) {
        return [id[0], id.slice(1)];
    }
    EntityReferences.split = split;
    /** used by the transformer to figure out where to check for the existence in a db of a concrete element id
     * @internal
     */
    function isValid(id) {
        return core_bentley_1.Id64.isValid(toId64(id));
    }
    EntityReferences.isValid = isValid;
    /** create the invalid id for a concrete entity type
     * @internal
     */
    function makeInvalid(type) {
        return `${type}${core_bentley_1.Id64.invalid}`;
    }
    EntityReferences.makeInvalid = makeInvalid;
    /** create an EntityReference given an entity */
    function from(entity) {
        const type = typeFromClass(entity.constructor);
        return `${type}${entity.id}`;
    }
    EntityReferences.from = from;
    /** create an EntityReference given an id and a JavaScript class */
    function fromClass(id, entityClass) {
        const type = typeFromClass(entityClass);
        return `${type}${id}`;
    }
    EntityReferences.fromClass = fromClass;
    /** Create an EntityReference quickly from an exact reference type and id */
    function fromEntityType(id, type) {
        return `${type}${id}`;
    }
    EntityReferences.fromEntityType = fromEntityType;
    /** @internal the argument entityClass be concrete (i.e. not the Entity abstract base class) */
    function typeFromClass(entityClass) {
        if (entityClass.is(Element_1.Element))
            return core_common_1.ConcreteEntityTypes.Element;
        else if (entityClass.is(ElementAspect_1.ElementAspect))
            return core_common_1.ConcreteEntityTypes.ElementAspect;
        else if (entityClass.is(Model_1.Model))
            return core_common_1.ConcreteEntityTypes.Model;
        else if (entityClass.is(Relationship_1.Relationship))
            return core_common_1.ConcreteEntityTypes.Relationship;
        else
            assert(false, "unknown or abstract entity type passed to EntityReferences.from");
    }
    EntityReferences.typeFromClass = typeFromClass;
})(EntityReferences || (exports.EntityReferences = EntityReferences = {}));
//# sourceMappingURL=EntityReferences.js.map