"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 ElementState
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElementState = exports.EntityState = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
/** The "state" of an Entity as represented in a web browser. Every subclass of EntityState handles one BIS class.
 * @public
 * @extensions
 */
class EntityState {
    /** The name of the BIS schema for this class.
     * @note Subclasses from other than the BisCore domain must override the static member "schemaName" with their schema name.
     */
    static get schemaName() { return "BisCore"; }
    get _ctor() { return this.constructor; }
    /** The name of the BIS class associated with this class.
     * @note Every subclass of EntityState **MUST** override this method to identify its BIS class.
     * Failure to do so will ordinarily result in an error when the class is registered, since there may only
     * be one JavaScript class for a given BIS class (usually the errant class will collide with its superclass.)
     */
    static get className() { return "Entity"; }
    /** The name of the BIS class associated with this class. */
    get className() { return this._ctor.className; }
    /** The Id of this Entity. May be invalid if the Entity has not yet been saved in the database. */
    id;
    /** The iModel from which this Entity was loaded */
    iModel;
    /** The full class name in the form "schema:class". */
    classFullName;
    /** Optional [json properties]($docs/bis/guide/fundamentals/element-fundamentals.md#jsonproperties) of this Entity. */
    jsonProperties;
    /** Constructor for EntityState
     * @param props the properties of the Entity for this EntityState
     * @param iModel the iModel from which this EntityState is to be constructed
     * @param _state source EntityState for clone
     */
    constructor(props, iModel, _state) {
        this.classFullName = props.classFullName ? props.classFullName : this._ctor.classFullName;
        this.iModel = iModel;
        this.id = core_bentley_1.Id64.fromJSON(props.id);
        this.jsonProperties = props.jsonProperties ? JSON.parse(JSON.stringify(props.jsonProperties)) : {}; // make sure we have our own copy
    }
    /** @internal */
    toJSON() {
        const val = {};
        val.classFullName = this.classFullName;
        if (core_bentley_1.Id64.isValid(this.id))
            val.id = this.id;
        if (this.jsonProperties && Object.keys(this.jsonProperties).length > 0)
            val.jsonProperties = this.jsonProperties;
        return val;
    }
    /** Return true if this EntityState is equal to another one. */
    equals(other) { return JSON.stringify(this.toJSON()) === JSON.stringify(other.toJSON()); }
    /** Make an independent copy of this EntityState */
    clone(iModel) { return new this._ctor(this.toJSON(), iModel ? iModel : this.iModel, this); }
    /** Get full BIS class name of this Entity in the form "SchemaName:ClassName".  */
    static get classFullName() { return `${this.schemaName}:${this.className}`; }
}
exports.EntityState = EntityState;
/** The "state" of an Element as represented in a web browser.
 * @public
 * @extensions
 */
class ElementState extends EntityState {
    static get className() { return "Element"; }
    /** The ModelId of the [Model]($docs/bis/guide/fundamentals/model-fundamentals.md) containing this element */
    model;
    /** The [Code]($docs/bis/guide/fundamentals/codes.md) for this element */
    code;
    /** The parent Element of this, or undefined if no parent. */
    parent;
    /** A [FederationGuid]($docs/bis/guide/fundamentals/element-fundamentals.md#federationguid) assigned to this element by some other federated database */
    federationGuid;
    /** A [user-assigned label]($docs/bis/guide/fundamentals/element-fundamentals.md#userlabel) for this element. */
    userLabel;
    constructor(props, iModel) {
        super(props, iModel);
        this.code = core_common_1.Code.fromJSON(props.code);
        this.model = core_common_1.RelatedElement.idFromJson(props.model);
        this.parent = core_common_1.RelatedElement.fromJSON(props.parent);
        if (undefined !== props.federationGuid)
            this.federationGuid = props.federationGuid;
        if (undefined !== props.userLabel)
            this.userLabel = props.userLabel;
    }
    /** Obtain this element's JSON representation. Subclasses of ElementState typically override this method with a more
     * specific return type.
     */
    toJSON() {
        const val = super.toJSON();
        if (core_bentley_1.Id64.isValid(this.code.spec))
            val.code = this.code;
        val.model = this.model;
        val.parent = this.parent;
        val.federationGuid = this.federationGuid;
        val.userLabel = this.userLabel;
        return val;
    }
}
exports.ElementState = ElementState;
//# sourceMappingURL=EntityState.js.map