/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { BentleyError, IModelStatus } from "@itwin/core-bentley";
/** @alpha */
export var CodeService;
(function (CodeService) {
    /** @internal */
    const codeSequences = new Map();
    /** Register an instance of a`CodeSequence` so it can be looked up by name. */
    function registerSequence(seq) {
        codeSequences.set(seq.sequenceName, seq);
    }
    CodeService.registerSequence = registerSequence;
    /** Get a previously registered `CodeSequence` by its name.
     * @throws if no sequence by that name was registered.
     */
    function getSequence(name) {
        const seq = codeSequences.get(name);
        if (!seq)
            throw new Error("SequenceNotFound", -1, `code sequence ${name} not found`);
        return seq;
    }
    CodeService.getSequence = getSequence;
    /**
     * Turn a `CodePops` for the briefcase of this CodeService into a `ScopeAndSpec` object for use with a CodeService.
     * This is necessary because the `spec` member of `CodeProps` refers to the id of a code spec in the iModel, and
     * the `scope` member refers to the element Id of the scope element in the iModel. This helper function
     * converts the spec Id to the spec name and looks up the `FederationGuid` of the scope element.
     */
    function makeScopeAndSpec(iModel, code) {
        const scopeGuid = iModel.elements.getElementProps({ id: code.scope, onlyBaseProperties: true }).federationGuid;
        if (undefined === scopeGuid)
            throw new CodeService.Error("MissingGuid", IModelStatus.InvalidCode, "code scope element has no federationGuid");
        return { scopeGuid, specName: iModel.codeSpecs.getById(code.spec).name };
    }
    CodeService.makeScopeAndSpec = makeScopeAndSpec;
    /** Turn a `CodeProps` and  `ProposedCodeProps` into a `ProposedCode` for use with a CodeService.
     * @see [[makeScopeAndSpec]] for explanation of why this is necessary.
     */
    function makeProposedCode(arg) {
        return {
            ...arg.props,
            value: arg.code.value,
            ...makeScopeAndSpec(arg.iModel, arg.code),
        };
    }
    CodeService.makeProposedCode = makeProposedCode;
    /** Exception class thrown by `CodeService` methods. */
    class Error extends BentleyError {
        /** A string that indicates the type of problem that caused the exception. */
        errorId;
        /** For [[CodeService.reserveCodes]] and [[CodeService.updateCodes]], a list of the problem details. */
        problems;
        /** @internal */
        constructor(errorId, errNum, message, problems) {
            super(errNum, message);
            this.errorId = errorId;
            this.problems = problems;
        }
    }
    CodeService.Error = Error;
})(CodeService || (CodeService = {}));
//# sourceMappingURL=CodeService.js.map