"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.getSettingsEditorForITwin = getSettingsEditorForITwin;
exports.constructSettingsEditorForITwin = constructSettingsEditorForITwin;
const core_common_1 = require("@itwin/core-common");
const IModelHost_1 = require("../../IModelHost");
const SettingsDb_1 = require("../../workspace/SettingsDb");
const SettingsEditor_1 = require("../../workspace/SettingsEditor");
const BlobContainerService_1 = require("../../BlobContainerService");
const WorkspaceImpl_1 = require("./WorkspaceImpl");
/** Obtain a [[WorkspaceEditor]] targeting the single existing settings container for a given iTwin.
 * Returns `undefined` if no container exists. Throws if multiple containers are found.
 * @internal
 */
async function getSettingsEditorForITwin(iTwinId) {
    const containerId = await SettingsEditor_1.SettingsContainers.getITwinContainerId(iTwinId);
    if (undefined === containerId)
        return undefined;
    const editor = (0, WorkspaceImpl_1.constructWorkspaceEditor)();
    try {
        const userToken = await IModelHost_1.IModelHost.getAccessToken();
        const tokenProps = await BlobContainerService_1.BlobContainer.service?.requestToken({ accessLevel: "write", containerId, userToken });
        if (!tokenProps)
            core_common_1.ITwinSettingsError.throwError("failed-to-obtain-container-token", { message: `Failed to obtain access token for iTwin settings container '${containerId}'.`, iTwinId });
        const container = editor.getContainer({
            accessToken: tokenProps.token,
            baseUri: tokenProps.baseUri,
            containerId,
            storageType: tokenProps.provider,
            writeable: true,
        });
        return { editor, container };
    }
    catch (error) {
        editor.close();
        throw error;
    }
}
/** Construct a [[WorkspaceEditor]] targeting the single settings container for a given iTwin.
 * If no container exists, one is created with default metadata. Throws if multiple containers are found.
 * @internal
 */
async function constructSettingsEditorForITwin(iTwinId) {
    const existing = await getSettingsEditorForITwin(iTwinId);
    if (existing)
        return existing;
    const editor = (0, WorkspaceImpl_1.constructWorkspaceEditor)();
    try {
        const container = await editor.createNewCloudContainer({
            scope: { iTwinId },
            metadata: {
                label: "iTwin settings",
                description: `Default settings container for iTwin ${iTwinId}`,
            },
            containerType: "settings",
            dbName: SettingsDb_1.settingsWorkspaceDbName,
            manifest: { workspaceName: `iTwin ${iTwinId} settings` },
        });
        return { editor, container };
    }
    catch (error) {
        editor.close();
        throw error;
    }
}
//# sourceMappingURL=SettingsEditorImpl.js.map