/*---------------------------------------------------------------------------------------------
* 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
 */
import { BentleyError, Logger } from "@itwin/core-bentley";
import { CloudSqlite } from "./CloudSqlite";
import { IModelHost } from "./IModelHost";
import { IModelNative } from "./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
 */
export 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.appWorkspace.settings.getBoolean(GeoCoordConfig.settingName.disableWorkspaces, false))
            return;
        try {
            const ws = 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) {
                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 (!IModelNative.platform.addGcsWorkspaceDb(gcsDbName, cloudContainer, dbProps.priority))
                return; // already had this db
            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.startCloudPrefetch(cloudContainer, gcsDbName));
        }
        catch (e) {
            let msg = `Cannot load GCS workspace (${e.errorNumber}): ${BentleyError.getErrorMessage(e)}`;
            msg += `,container=${dbProps.baseUri}/${dbProps.containerId}, storage=${dbProps.storageType}, public=${dbProps.isPublic}, cacheDir=${IModelHost.cacheDir}`;
            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.appWorkspace.settings, this.settingName.defaultDatabases);
        }
    }
    static loadForImodel(settings) {
        this.loadDefaultDatabases();
        this.loadAll(settings, this.settingName.databases);
    }
}
//# sourceMappingURL=GeoCoordConfig.js.map