/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Elements
 */
import { Point3d } from "@itwin/core-geometry";
import { BisCodeSpec, Code, CodeScopeSpec, IModel, RelatedElement, } from "@itwin/core-common";
import { InformationReferenceElement, UrlLink } from "./Element";
import { EditTxn } from "./EditTxn";
import { ExternalSourceAttachmentAttachesSource, ExternalSourceIsInRepository } from "./NavigationRelationship";
import { _implicitTxn } from "./internal/Symbols";
/** An ExternalSource refers to an 'information container' found in a repository. In some cases, the container is the entire repository.
 * @note The associated ECClass was added to the BisCore schema in version 1.0.13
 * @beta
 */
export class ExternalSource extends InformationReferenceElement {
    /** The repository that contains this ExternalSource. */
    repository;
    /** The name of the iModel Connecter that processed this ExternalSource. */
    connectorName;
    /** The version of the iModel Connecter that processed this ExternalSource. */
    connectorVersion;
    /** @internal */
    static get className() { return "ExternalSource"; }
    constructor(props, iModel) {
        super(props, iModel);
        if (props.repository)
            this.repository = new ExternalSourceIsInRepository(RelatedElement.idFromJson(props.repository));
    }
    toJSON() {
        return super.toJSON(); // Entity.toJSON takes care of auto-handled properties
    }
    static ensureCodeSpec(txnOrIModel) {
        const txn = txnOrIModel instanceof EditTxn ? txnOrIModel : txnOrIModel[_implicitTxn];
        try {
            const codeSpec = txn.iModel.codeSpecs.getByName(BisCodeSpec.externalSource);
            return codeSpec.id;
        }
        catch {
            return txn.iModel.codeSpecs.insert(txn, BisCodeSpec.externalSource, CodeScopeSpec.Type.Repository);
        }
    }
    /** Create a Code for an ExternalSource element given a name that is meant to be unique within the scope of the iModel.
     * @param iModelDb  The IModelDb
     * @param codeValue The ExternalSource name
     * @see [[ensureCodeSpec]]
     */
    static createCode(iModelDb, codeValue) {
        const codeSpec = iModelDb.codeSpecs.getByName(BisCodeSpec.externalSource);
        return new Code({ spec: codeSpec.id, scope: IModel.rootSubjectId, value: codeValue });
    }
    collectReferenceIds(referenceIds) {
        super.collectReferenceIds(referenceIds);
        if (this.repository)
            referenceIds.addElement(this.repository.id);
    }
}
/** Attachment of an ExternalSource
 * @note The associated ECClass was added to the BisCore schema in version 1.0.13
 * @beta
 */
export class ExternalSourceAttachment extends InformationReferenceElement {
    /** The [[ExternalSource]] that is attached by this ExternalSourceAttachment. */
    attaches;
    /** Specifies whether the attached [[ExternalSource]] provides context or models a part of the whole. */
    role;
    /** The translation or offset in global coordinates of the attached [[ExternalSource]] relative to the ExternalSource that attaches it. */
    translation;
    /** The Yaw angle (in degrees) of the attached [[ExternalSource]] relative to the ExternalSource that attaches it. */
    yaw;
    /** The Pitch angle (in degrees) of the attached [[ExternalSource]] relative to the ExternalSource that attaches it. */
    pitch;
    /** The Roll angle (in degrees) of the attached [[ExternalSource]] relative to the ExternalSource that attaches it. */
    roll;
    /** The scale of the attached [[ExternalSource]] relative to the ExternalSource that attaches it. */
    scale;
    /** @internal */
    static get className() { return "ExternalSourceAttachment"; }
    constructor(props, iModel) {
        super(props, iModel);
        if (props.attaches)
            this.attaches = new ExternalSourceAttachmentAttachesSource(RelatedElement.idFromJson(props.attaches));
        if (props.translation)
            this.translation = Point3d.fromJSON(props.translation);
        if (props.scale)
            this.scale = Point3d.fromJSON(props.scale);
    }
    toJSON() {
        return super.toJSON(); // Entity.toJSON takes care of auto-handled properties
    }
    static ensureCodeSpec(txnOrIModel) {
        const txn = txnOrIModel instanceof EditTxn ? txnOrIModel : txnOrIModel[_implicitTxn];
        try {
            const codeSpec = txn.iModel.codeSpecs.getByName(BisCodeSpec.externalSourceAttachment);
            return codeSpec.id;
        }
        catch {
            return txn.iModel.codeSpecs.insert(txn, BisCodeSpec.externalSourceAttachment, CodeScopeSpec.Type.ParentElement);
        }
    }
    /** Create a Code for an ExternalSourceAttachment element given a name that is meant to be unique within the scope of its parent [[ExternalSource]].
     * @param iModelDb  The IModelDb
     * @param scopeElementId The parent ExternalSource
     * @param codeValue The ExternalSourceAttachment name
     * @see [[ensureCodeSpec]]
     */
    static createCode(iModelDb, scopeElementId, codeValue) {
        const codeSpec = iModelDb.codeSpecs.getByName(BisCodeSpec.externalSourceAttachment);
        return new Code({ spec: codeSpec.id, scope: scopeElementId, value: codeValue });
    }
}
/** A group of ExternalSources that are collectively a source of information for one or more elements.
 * @note The associated ECClass was added to the BisCore schema in version 1.0.13
 * @beta
 */
export class ExternalSourceGroup extends ExternalSource {
    /** @internal */
    static get className() { return "ExternalSourceGroup"; }
    constructor(props, iModel) {
        super(props, iModel);
    }
}
/** Link to the Configuration for an iModel Synchronization Job
 * @note The associated ECClass was added to the BisCore schema in version 1.0.13
 * @beta
 */
export class SynchronizationConfigLink extends UrlLink {
    /** Date/Time of last successful run of this synchronization configuration */
    lastSuccessfulRun;
    /** @internal */
    static get className() { return "SynchronizationConfigLink"; }
    constructor(props, iModel) {
        super(props, iModel);
    }
    toJSON() {
        return super.toJSON(); // Entity.toJSON takes care of auto-handled properties
    }
}
//# sourceMappingURL=ExternalSource.js.map