/*---------------------------------------------------------------------------------------------
 * 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
 */
import { DialogButtonType } from "@itwin/appui-abstract";
import * as React from "react";
import { UiFramework } from "../UiFramework";
import { getUniqueId } from "../layout/base/NineZone";
import { ComponentGenerator } from "../uiprovider/ComponentGenerator";
import { DefaultDialogGridContainer } from "../uiprovider/DefaultDialogGridContainer";
import { Button, Dialog } from "@itwin/itwinui-react";
import classnames from "classnames";
import { useTranslation } from "../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.
 */
export function UiDataProvidedDialog({ uiDataProvider, id, isModal, title, movable, resizable, minWidth, minHeight, maxWidth, maxHeight, width, height, style, }) {
    const dialogId = React.useRef(id ? id : getUniqueId());
    const dialogIsModal = React.useRef(isModal);
    const onOK = React.useRef();
    const onCancel = React.useRef();
    const closeDialog = () => {
        if (dialogIsModal.current)
            UiFramework.dialogs.modal.close();
        else
            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 (DialogButtonType.Cancel === button.type) {
                    onCancel.current = button.onClick;
                    button.onClick = handleCancel;
                    continue;
                }
                if (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(() => {
        return uiDataProvider.onItemsReloadedEvent.addListener(() => {
            setButtonCluster(generateButtonCluster(uiDataProvider.supplyButtonData()));
        });
    }, [generateButtonCluster, uiDataProvider]);
    React.useEffect(() => {
        return uiDataProvider.onButtonsReloadedEvent.addListener(() => {
            setButtonCluster(generateButtonCluster(uiDataProvider.supplyButtonData()));
        });
    }, [generateButtonCluster, uiDataProvider]);
    const handleClose = React.useCallback(() => closeDialog(), []);
    const containerStyle = {
        minWidth,
        minHeight,
        maxWidth: maxWidth || "100%",
        maxHeight,
        width: width || "50%",
        margin: "",
        height,
    };
    return (React.createElement(Dialog, { isOpen: true, onClose: handleClose, closeOnEsc: true, closeOnExternalClick: false, isDraggable: movable, isResizable: resizable, preventDocumentScroll: true, style: style },
        isModal && React.createElement(Dialog.Backdrop, null),
        React.createElement(Dialog.Main, { style: containerStyle, "data-testid": "ui-data-provided-dialog-container" },
            React.createElement(Dialog.TitleBar, { titleText: title }),
            React.createElement(Dialog.Content, null,
                React.createElement(DefaultDialogGridContainer, { componentGenerator: new ComponentGenerator(uiDataProvider) })),
            React.createElement(Dialog.ButtonBar, null, buttonCluster &&
                buttonCluster.map((button, index) => (React.createElement(DialogButton, { key: index, button: button })))))));
}
function DialogButton({ button }) {
    const { translate } = useTranslation();
    let buttonText = "";
    let usePrimaryStyleType = false;
    let buttonClass = button.className;
    switch (button.type) {
        case DialogButtonType.OK:
            buttonText = translate("dialog.ok");
            buttonClass = classnames(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
        case DialogButtonType.Retry:
            buttonText = translate("dialog.retry");
            buttonClass = classnames(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
        case DialogButtonType.Yes:
            buttonText = translate("dialog.yes");
            buttonClass = classnames(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
        case DialogButtonType.No:
            buttonText = translate("dialog.no");
            buttonClass = classnames(buttonClass, button.buttonStyle);
            break;
        case DialogButtonType.Cancel:
            buttonText = translate("dialog.cancel");
            buttonClass = classnames(buttonClass, button.buttonStyle);
            break;
        case DialogButtonType.Close:
            buttonText = translate("dialog.close");
            buttonClass = classnames(buttonClass, button.buttonStyle);
            break;
        case DialogButtonType.Next:
            buttonText = translate("dialog.next");
            buttonClass = classnames(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
        case DialogButtonType.Previous:
            buttonText = translate("dialog.previous");
            buttonClass = classnames(buttonClass, button.buttonStyle);
            usePrimaryStyleType = true;
            break;
    }
    buttonText = button.label || buttonText;
    return (React.createElement(Button, { className: buttonClass, disabled: button.disabled, styleType: usePrimaryStyleType ? "high-visibility" : undefined, onClick: button.onClick }, buttonText));
}
//# sourceMappingURL=UiDataProvidedDialog.js.map