"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 Workspace
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Workspace = exports.WorkspaceSettingNames = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const BackendLoggerCategory_1 = require("../BackendLoggerCategory");
const Symbols_1 = require("../internal/Symbols");
function makeSettingName(name) {
    return `${"itwin/core/workspace"}/${name}`;
}
/** The names of various [[Setting]]s with special meaning to the [[Workspace]] system.
 * @beta
 */
var WorkspaceSettingNames;
(function (WorkspaceSettingNames) {
    /** The name of a setting that, when present in a [[WorkspaceDb]] loaded by [[Workspace.loadSettingsDictionary]], will automatically
     * be used to find and load additional [[SettingsDictionary]]'s in other [[WorkspaceDb]]s. This permits you to chain the settings inside on [[WorkspaceDb]]
     * to others upon which they depend.
     * This setting's value is an array of [[WorkspaceDbSettingsProps]]s.
     */
    WorkspaceSettingNames.settingsWorkspaces = makeSettingName("settingsWorkspaces");
})(WorkspaceSettingNames || (exports.WorkspaceSettingNames = WorkspaceSettingNames = {}));
function getWorkspaceResource(dbs, name, type) {
    for (const db of dbs) {
        const val = type === "blob" ? db.getBlob(name) : db.getString(name);
        if (undefined !== val) {
            return val;
        }
    }
    return undefined;
}
/** @beta */
var Workspace;
(function (Workspace) {
    /** A function invoked to handle exceptions produced while loading workspace data.
     * Applications can override this function to notify the user and/or attempt to diagnose the problem.
     * The default implementation simply logs each exception.
     */
    Workspace.exceptionDiagnosticFn = (e) => {
        if (e instanceof Error)
            core_bentley_1.Logger.logError(BackendLoggerCategory_1.BackendLoggerCategory.Workspace, e);
        else
            core_bentley_1.UnexpectedErrors.handle(e);
    };
    /** A function invoked each time any [[SettingsDictionary]] is loaded from a [[WorkspaceDb]].
     * Applications can override this function to notify the user and/or record diagnostics.
     * The default implementation simply records an information message in the [Logger]($bentley).
     */
    Workspace.onSettingsDictionaryLoadedFn = (loaded) => {
        core_bentley_1.Logger.logInfo(BackendLoggerCategory_1.BackendLoggerCategory.Workspace, `loaded setting dictionary ${loaded.dict.props.name} from ${loaded.from.dbFileName}`);
    };
    /** Searches a list of [[WorkspaceDb]]s for a string resource of a given name.
     * The list is searched in order, and the first resource with the request name is returned.
     * If no such resource exists, the function returns `undefined`.
     * @see [[WorkspaceDb.getString]] if you only need to search a single `WorkspaceDb`.
     * @beta
     */
    function getStringResource(args) {
        return getWorkspaceResource(args.dbs, args.name, "string");
    }
    Workspace.getStringResource = getStringResource;
    /** Searches a list of [[WorkspaceDb]]s for a blob resource of a given name.
     * The list is searched in order, and the first resource with the request name is returned.
     * If no such resource exists, the function returns `undefined`.
     * @see [[WorkspaceDb.getblob]] if you only need to search a single `WorkspaceDb`.
     * @beta
     */
    function getBlobResource(args) {
        return getWorkspaceResource(args.dbs, args.name, "blob");
    }
    Workspace.getBlobResource = getBlobResource;
    /** Query a list of [[WorkspaceDb]]s to find resources of a particular type with names matching a specified pattern.
     * @see [[WorkspaceDb.queryResources]] if you only need to query a single `WorkspaceDb`.
     * @beta
     */
    function queryResources(args) {
        const resources = [];
        for (const db of args.dbs) {
            db.queryResources({
                type: args.type,
                namePattern: args.namePattern,
                nameCompare: args.nameCompare,
                callback: (names) => {
                    for (const name of names) {
                        resources.push({ db, name });
                    }
                },
            });
        }
        args.callback(resources);
    }
    Workspace.queryResources = queryResources;
})(Workspace || (exports.Workspace = Workspace = {}));
//# sourceMappingURL=Workspace.js.map