"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 ToolSettings
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultToolSettingsProvider = void 0;
const React = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const appui_abstract_1 = require("@itwin/appui-abstract");
const ComponentGenerator_1 = require("../uiprovider/ComponentGenerator");
const DefaultDialogGridContainer_1 = require("../uiprovider/DefaultDialogGridContainer");
const ToolUiProvider_1 = require("./ToolUiProvider");
const UiFramework_1 = require("../UiFramework");
const core_bentley_1 = require("@itwin/core-bentley");
/** @internal */
/** ToolSettingsUiDataProvider keeps tool data in sync with UI display */
class ToolSettingsUiDataProvider extends appui_abstract_1.UiLayoutDataProvider {
    constructor() {
        super(...arguments);
        // send property changes from UI back to tool
        this.applyUiPropertyChange = (syncItem) => {
            var _a;
            // istanbul ignore next
            (_a = core_frontend_1.IModelApp.toolAdmin.activeTool) === null || _a === void 0 ? void 0 : _a.applyToolSettingPropertyChange(syncItem).catch((err) => core_bentley_1.UnexpectedErrors.handle(err));
        };
    }
    supplyDialogItems() {
        return UiFramework_1.UiFramework.toolSettings.toolSettingsProperties;
    }
}
/** ToolUiProvider class that informs ConfigurableUi that Tool Settings are provided for the specified tool.
 * @internal
 */
class DefaultToolSettingsProvider extends ToolUiProvider_1.ToolUiProvider {
    constructor(info, options) {
        super(info, options);
        this._dataProvider = new ToolSettingsUiDataProvider();
    }
    get uiDataProvider() {
        return this._dataProvider;
    }
    updateToolSettingsNodes() {
        // istanbul ignore else
        if (this.uiDataProvider.layoutDialogRows()) {
            const componentGenerator = new ComponentGenerator_1.ComponentGenerator(this.uiDataProvider);
            this.toolSettingsNode = (React.createElement(DefaultDialogGridContainer_1.DefaultDialogGridContainer, { componentGenerator: componentGenerator, isToolSettings: true }));
            this.horizontalToolSettingNodes =
                componentGenerator.getToolSettingsEntries();
        }
        else {
            this.toolSettingsNode = null;
            this.horizontalToolSettingNodes = [];
        }
        // We emit the UiFramework.frontstages.onToolSettingsReloadEvent event here because the Frontstage Manager is a static
        // class that UI components can easily register listeners.
        UiFramework_1.UiFramework.frontstages.onToolSettingsReloadEvent.emit();
    }
    reloadPropertiesFromTool() {
        // trigger the items to be reloaded from the active tool
        this.uiDataProvider.reloadDialogItems(false);
        this.updateToolSettingsNodes();
    }
    // called when tool is activated to repopulate tool settings.
    onInitialize() {
        this.reloadPropertiesFromTool();
    }
    // called to process UiFramework.toolSettings.onSyncToolSettingsProperties event
    syncToolSettingsProperties(args) {
        this.uiDataProvider.fireSyncPropertiesEvent(args.syncProperties);
    }
}
exports.DefaultToolSettingsProvider = DefaultToolSettingsProvider;
UiFramework_1.UiFramework.controls.register("DefaultToolSettings", DefaultToolSettingsProvider);
//# sourceMappingURL=DefaultToolSettingsProvider.js.map