"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 iModels
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeoCoordConfig = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const CloudSqlite_1 = require("./CloudSqlite");
const IModelHost_1 = require("./IModelHost");
const NativePlatform_1 = require("./internal/NativePlatform");
const loggerCat = "GeoCoord";
const makeSettingName = (name) => `${"itwin/core/gcs"}/${name}`;
/**
 * Internal class to configure and load the gcs workspaces for an iModel.
 * @internal
 */
class GeoCoordConfig {
    /** array of cloud prefetch tasks that may be awaited to permit offline usage */
    static prefetches = [];
    static settingName = {
        databases: makeSettingName("databases"),
        defaultDatabases: makeSettingName("default/databases"),
        disableWorkspaces: makeSettingName("disableWorkspaces"),
    };
    static addGcsWorkspace(dbProps) {
        // override to disable loading GCS data from workspaces
        if (IModelHost_1.IModelHost.appWorkspace.settings.getBoolean(GeoCoordConfig.settingName.disableWorkspaces, false))
            return;
        try {
            const ws = IModelHost_1.IModelHost.appWorkspace;
            const container = ws.getContainer({ ...dbProps, accessToken: "" }); // all gcs containers are public so no accessToken is required
            const cloudContainer = container.cloudContainer;
            if (!cloudContainer?.isConnected) {
                core_bentley_1.Logger.logError("GeoCoord", `could not load gcs database "${dbProps.dbName}"`);
                return;
            }
            const gcsDbName = container.resolveDbFileName(dbProps);
            const gcsDbProps = cloudContainer.queryDatabase(gcsDbName);
            if (undefined === gcsDbProps)
                throw new Error(`database "${gcsDbName}" not found in container "${dbProps.containerId}"`);
            if (!NativePlatform_1.IModelNative.platform.addGcsWorkspaceDb(gcsDbName, cloudContainer, dbProps.priority))
                return; // already had this db
            core_bentley_1.Logger.logInfo(loggerCat, `loaded gcsDb "${gcsDbName}", from "${dbProps.baseUri}/${dbProps.containerId}" size=${gcsDbProps.totalBlocks}, local=${gcsDbProps.localBlocks}`);
            if (true === dbProps.prefetch)
                this.prefetches.push(CloudSqlite_1.CloudSqlite.startCloudPrefetch(cloudContainer, gcsDbName));
        }
        catch (e) {
            let msg = `Cannot load GCS workspace (${e.errorNumber}): ${core_bentley_1.BentleyError.getErrorMessage(e)}`;
            msg += `,container=${dbProps.baseUri}/${dbProps.containerId}, storage=${dbProps.storageType}, public=${dbProps.isPublic}, cacheDir=${IModelHost_1.IModelHost.cacheDir}`;
            core_bentley_1.Logger.logError(loggerCat, msg);
        }
    }
    static loadAll(settings, settingName) {
        const dbProps = settings.getArray(settingName);
        if (dbProps) {
            for (const entry of dbProps) {
                this.addGcsWorkspace(entry);
            }
        }
    }
    static _defaultDbsLoaded = false;
    static onStartup() {
        this._defaultDbsLoaded = false;
        this.prefetches.length = 0;
    }
    static loadDefaultDatabases() {
        if (!this._defaultDbsLoaded) {
            this._defaultDbsLoaded = true;
            this.loadAll(IModelHost_1.IModelHost.appWorkspace.settings, this.settingName.defaultDatabases);
        }
    }
    static loadForImodel(settings) {
        this.loadDefaultDatabases();
        this.loadAll(settings, this.settingName.databases);
    }
}
exports.GeoCoordConfig = GeoCoordConfig;
//# sourceMappingURL=GeoCoordConfig.js.map