"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IModelIncrementalSchemaLocater = void 0;
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module iModels
 */
const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
const core_common_1 = require("@itwin/core-common");
/**
 * A [[ECSqlSchemaLocater]]($ecschema-metadata) implementation that uses the [[IModelDb]] to load schemas incrementally.
 * @beta
 */
class IModelIncrementalSchemaLocater extends ecschema_metadata_1.ECSqlSchemaLocater {
    _iModel;
    /**
     * Constructs a new IModelIncrementalSchemaLocater instance.
     * @param iModel The [[IModelDb]] to query.
     * @param options Optional [[ECSqlSchemaLocaterOptions]]($ecschema-metadata).
     */
    constructor(iModel, options) {
        super(options ?? { useMultipleQueries: true });
        this._iModel = iModel;
    }
    /**
     * Gets [[SchemaProps]]($ecschema-metadata) for the given [[SchemaKey]]($ecschema-metadata).
     * This is the full schema json with all elements that are defined in the schema.
     * @param schemaKey The key of the schema to be resolved.
     */
    async getSchemaProps(schemaKey) {
        // To keep the main thread from being blocked in sync loading cases the resolving
        // is triggered through a timeout. Even if there is no delay, it improves loading
        // time by ~3x.
        return new Promise((resolve, reject) => setTimeout(() => {
            try {
                resolve(this._iModel.getSchemaProps(schemaKey.name));
            }
            catch (error) {
                reject(error);
            }
        }, 0));
    }
    /**
     * Executes an ECSql query against the IModelDb.
     * @param query The query to execute
     * @param options The [[ECSqlQueryOptions]]($ecschema-metadata) to use.
     * @returns A promise that resolves to read-only array of type TRow.
     */
    async executeQuery(query, options) {
        const queryParameters = options && options.parameters ? core_common_1.QueryBinder.from(options.parameters) : undefined;
        return this._iModel
            .createQueryReader(query, queryParameters, {
            rowFormat: core_common_1.QueryRowFormat.UseECSqlPropertyNames,
            limit: { count: options?.limit },
        })
            .toArray();
    }
}
exports.IModelIncrementalSchemaLocater = IModelIncrementalSchemaLocater;
//# sourceMappingURL=IModelIncrementalSchemaLocater.js.map