"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 Dialog
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.UiDataProvidedDialog = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const React = require("react");
const UiFramework_1 = require("../UiFramework");
const NineZone_1 = require("../layout/base/NineZone");
const ComponentGenerator_1 = require("../uiprovider/ComponentGenerator");
const DefaultDialogGridContainer_1 = require("../uiprovider/DefaultDialogGridContainer");
const itwinui_react_1 = require("@itwin/itwinui-react");
const classnames_1 = require("classnames");
const useTranslation_1 = require("../hooks/useTranslation");
/** Component to show dialog populated from properties supplied via uiDataProvider
 * @public
 * @deprecated in 4.15.0. Use {@link https://itwinui.bentley.com/docs/dialog iTwinUI Dialog} instead.
 */
function UiDataProvidedDialog({ uiDataProvider, id, isModal, title, movable, resizable, minWidth, minHeight, maxWidth, maxHeight, width, height, style, }) {
    const dialogId = React.useRef(id ? id : (0, NineZone_1.getUniqueId)());
    const dialogIsModal = React.useRef(isModal);
    const onOK = React.useRef();
    const onCancel = React.useRef();
    const closeDialog = () => {
        if (dialogIsModal.current)
            UiFramework_1.UiFramework.dialogs.modal.close();
        else
            UiFramework_1.UiFramework.dialogs.modeless.close(dialogId.current);
    };
    const handleOk = React.useCallback(() => {
        onOK.current && onOK.current();
        closeDialog();
    }, []);
    const handleCancel = React.useCallback(() => {
        onCancel.current && onCancel.current();
        closeDialog();
    }, []);
    const generateButtonCluster = React.useCallback((buttons) => {
        if (buttons) {
            for (const button of buttons) {
                if (appui_abstract_1.DialogButtonType.Cancel === button.type) {
                    onCancel.current = button.onClick;
                    button.onClick = handleCancel;
                    continue;
                }
                if (appui_abstract_1.DialogButtonType.OK === button.type) {
                    onOK.current = button.onClick;
                    button.onClick = handleOk;
                    continue;
                }
            }
        }
        return buttons;
    }, [handleCancel, handleOk]);
    const [buttonCluster, setButtonCluster] = React.useState(() => generateButtonCluster(uiDataProvider.supplyButtonData()));
    React.useEffect(() => {
        const handleReloaded = () => {
            setButtonCluster(generateButtonCluster(uiDataProvider.supplyButtonData()));
        };
        uiDataProvider.onItemsReloadedEvent.addListener(handleReloaded);
        return () => {
            uiDataProvider.onItemsReloadedEvent.removeListener(handleReloaded);
        };
    }, [generateButtonCluster, uiDataProvider]);
    React.useEffect(() => {
        const handleButtonReloaded = () => {
            setButtonCluster(generateButtonCluster(uiDataProvider.supplyButtonData()));
        };
        uiDataProvider.onButtonsReloadedEvent.addListener(handleButtonReloaded);
        return () => {
            uiDataProvider.onButtonsReloadedEvent.removeListener(handleButtonReloaded);
        };
    }, [generateButtonCluster, uiDataProvider]);
    const handleClose = React.useCallback(() => closeDialog(), []);
    const containerStyle = {
        minWidth,
        minHeight,
        maxWidth: maxWidth || "100%",
        maxHeight,
        width: width || "50%",
        margin: "",
        height,
    };
    return (React.createElement(itwinui_react_1.Dialog, { isOpen: true, onClose: handleClose, closeOnEsc: true, closeOnExternalClick: false, isDraggable: movable, isResizable: resizable, preventDocumentScroll: true, style: style },
        isModal && React.createElement(itwinui_react_1.Dialog.Backdrop, null),
        React.createElement(itwinui_react_1.Dialog.Main, { style: containerStyle, "data-testid": "ui-data-provided-dialog-container" },
            React.createElement(itwinui_react_1.Dialog.TitleBar, { titleText: title }),
            React.createElement(itwinui_react_1.Dialog.Content, null,
                React.createElement(DefaultDialogGridContainer_1.DefaultDialogGridContainer, { componentGenerator: new ComponentGenerator_1.ComponentGenerator(uiDataProvider) })),
            React.createElement(itwinui_react_1.Dialog.ButtonBar, null, buttonCluster &&
                buttonCluster.map((button, index) => (React.createElement(DialogButton, { key: index, button: button })))))));
}
exports.UiDataProvidedDialog = UiDataProvidedDialog;
function DialogButton({ button }) {
    const { translate } = (0, useTranslation_1.useTranslation)();
    let buttonText = "";
    let usePrimaryStyleType = false;
    let buttonClass = button.className;
    switch (button.type) {
        case appui_abstract_1.DialogButtonType.OK:
            buttonText = translate("dialog.ok");
            buttonClass = (0, classnames_1.default)(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
        case appui_abstract_1.DialogButtonType.Retry:
            buttonText = translate("dialog.retry");
            buttonClass = (0, classnames_1.default)(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
        case appui_abstract_1.DialogButtonType.Yes:
            buttonText = translate("dialog.yes");
            buttonClass = (0, classnames_1.default)(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
        case appui_abstract_1.DialogButtonType.No:
            buttonText = translate("dialog.no");
            buttonClass = (0, classnames_1.default)(buttonClass, button.buttonStyle);
            break;
        case appui_abstract_1.DialogButtonType.Cancel:
            buttonText = translate("dialog.cancel");
            buttonClass = (0, classnames_1.default)(buttonClass, button.buttonStyle);
            break;
        case appui_abstract_1.DialogButtonType.Close:
            buttonText = translate("dialog.close");
            buttonClass = (0, classnames_1.default)(buttonClass, button.buttonStyle);
            break;
        case appui_abstract_1.DialogButtonType.Next:
            buttonText = translate("dialog.next");
            buttonClass = (0, classnames_1.default)(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
        case appui_abstract_1.DialogButtonType.Previous:
            buttonText = translate("dialog.previous");
            buttonClass = (0, classnames_1.default)(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
    }
    buttonText = button.label || buttonText;
    return (React.createElement(itwinui_react_1.Button, { className: buttonClass, disabled: button.disabled, styleType: usePrimaryStyleType ? "high-visibility" : undefined, onClick: button.onClick }, buttonText));
}
//# sourceMappingURL=UiDataProvidedDialog.js.map