import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { memo, useCallback, useMemo } from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { usePropState } from "@gooddata/sdk-ui";
import { UiMenu } from "../@ui/UiMenu/UiMenu.js";
import { Dropdown } from "../Dropdown/Dropdown.js";
import { DropdownButton } from "../Dropdown/DropdownButton.js";
import { Checkbox } from "../Form/Checkbox.js";
import { Overlay } from "../Overlay/Overlay.js";
import { useIdPrefixed } from "../utils/useId.js";
import { ConfirmDialogBase } from "./ConfirmDialogBase.js";
const alignPoints = [{ align: "cc cc" }];
const getPageSizeMenuItems = (intl) => [
    {
        type: "interactive",
        id: "A3",
        stringTitle: intl.formatMessage({ id: "dialogs.export.pdf.pageSize.option.a3" }),
        data: "A3",
    },
    {
        type: "interactive",
        id: "A4",
        stringTitle: intl.formatMessage({ id: "dialogs.export.pdf.pageSize.option.a4" }),
        data: "A4",
    },
    {
        type: "interactive",
        id: "Letter",
        stringTitle: intl.formatMessage({ id: "dialogs.export.pdf.pageSize.option.letter" }),
        data: "LETTER",
    },
];
const getOrientationMenuItems = (intl) => [
    {
        type: "interactive",
        id: "Portrait",
        stringTitle: intl.formatMessage({ id: "dialogs.export.pdf.pageOrientation.option.portrait" }),
        data: "PORTRAIT",
    },
    {
        type: "interactive",
        id: "Landscape",
        stringTitle: intl.formatMessage({ id: "dialogs.export.pdf.pageOrientation.option.landscape" }),
        data: "LANDSCAPE",
    },
];
/**
 * @internal
 */
export const ExportTabularPdfDialog = memo(function ExportTabularPdfDialog({ displayCloseButton = true, isPositive = true, isSubmitDisabled = false, cancelButtonText = "Cancel", submitButtonText = "Export", onCancel = () => { }, onSubmit = () => { }, pageSize = "A4", pageOrientation = "PORTRAIT", showInfoPage = true, isShowInfoPageVisible = true, }) {
    const intl = useIntl();
    const [selectedSize, setSelectedSize] = usePropState(pageSize);
    const [selectedOrientation, setSelectedOrientation] = usePropState(pageOrientation);
    const [shouldShowInfoPage, setShouldShowInfoPage] = usePropState(showInfoPage);
    const pageSizeMenuItems = useMemo(() => getPageSizeMenuItems(intl), [intl]);
    const orientationMenuItems = useMemo(() => getOrientationMenuItems(intl), [intl]);
    const dialogId = useIdPrefixed("pdfExportDialog");
    const showInfoPageId = useIdPrefixed("showInfoPage");
    const pageSizeDropdownButtonId = useIdPrefixed("pageSizeDropdownButton");
    const orientationDropdownButtonId = useIdPrefixed("orientationDropdownButton");
    const handleSubmit = useCallback(() => {
        onSubmit?.({
            pageSize: selectedSize,
            pageOrientation: selectedOrientation,
            showInfoPage: shouldShowInfoPage,
        });
    }, [selectedSize, selectedOrientation, shouldShowInfoPage, onSubmit]);
    const handlePageSizeChange = useCallback((value) => {
        setSelectedSize(value);
    }, [setSelectedSize]);
    const handleOrientationChange = useCallback((value) => {
        setSelectedOrientation(value);
    }, [setSelectedOrientation]);
    return (_jsx(Overlay, { alignPoints: alignPoints, isModal: true, positionType: "fixed", children: _jsxs(ConfirmDialogBase, { className: "gd-pdf-export-dialog", displayCloseButton: displayCloseButton, isPositive: isPositive, isSubmitDisabled: isSubmitDisabled, headline: intl.formatMessage({ id: "dialogs.export.pdf.headline" }), cancelButtonText: cancelButtonText || intl.formatMessage({ id: "cancel" }), submitButtonText: submitButtonText || intl.formatMessage({ id: "dialogs.export.submit" }), onCancel: onCancel, onSubmit: handleSubmit, autofocusOnOpen: true, accessibilityConfig: {
                dialogId,
            }, children: [
                _jsxs("div", { className: "gd-input gd-pdf-export-dialog-item", children: [
                        _jsx("label", { className: "gd-label", htmlFor: pageSizeDropdownButtonId, children: _jsx(FormattedMessage, { id: "dialogs.export.pdf.pageSize" }) }), _jsx(Dropdown, { fullscreenOnMobile: false, renderButton: ({ isOpen, toggleDropdown }) => (_jsx(DropdownButton, { id: pageSizeDropdownButtonId, value: pageSizeMenuItems.find((item) => item.data === selectedSize)?.stringTitle, isOpen: isOpen, onClick: toggleDropdown, accessibilityConfig: {
                                    ariaHaspopup: "true",
                                    ariaExpanded: isOpen,
                                    ...(isOpen ? { ariaControls: "page-size-menu" } : {}),
                                }, isFullWidth: true })), renderBody: ({ closeDropdown }) => (_jsx(UiMenu, { items: pageSizeMenuItems.map((item) => ({
                                    ...item,
                                    isSelected: selectedSize === item.data,
                                })), onSelect: (item) => {
                                    handlePageSizeChange(item.data);
                                }, onClose: closeDropdown, shouldCloseOnSelect: true, ariaAttributes: {
                                    "aria-label": "Page size options",
                                    id: "page-size-menu",
                                }, maxWidth: 150 })), autofocusOnOpen: true })] }), _jsxs("div", { className: "gd-input gd-pdf-export-dialog-item", children: [
                        _jsx("label", { className: "gd-label", htmlFor: orientationDropdownButtonId, children: _jsx(FormattedMessage, { id: "dialogs.export.pdf.pageOrientation" }) }), _jsx(Dropdown, { fullscreenOnMobile: false, renderButton: ({ isOpen, toggleDropdown }) => (_jsx(DropdownButton, { id: orientationDropdownButtonId, value: orientationMenuItems.find((item) => item.data === selectedOrientation)
                                    ?.stringTitle, isOpen: isOpen, onClick: toggleDropdown, accessibilityConfig: {
                                    ariaHaspopup: "true",
                                    ariaExpanded: isOpen,
                                    ...(isOpen ? { ariaControls: "orientation-menu" } : {}),
                                }, isFullWidth: true })), renderBody: ({ closeDropdown }) => (_jsx(UiMenu, { items: orientationMenuItems.map((item) => ({
                                    ...item,
                                    isSelected: selectedOrientation === item.data,
                                })), onSelect: (item) => {
                                    handleOrientationChange(item.data);
                                }, onClose: closeDropdown, shouldCloseOnSelect: true, ariaAttributes: {
                                    "aria-label": "Orientation options",
                                    id: "orientation-menu",
                                }, maxWidth: 150 })), autofocusOnOpen: true })] }), isShowInfoPageVisible === true && (_jsx("div", { className: "gd-pdf-export-dialog-item", children: _jsx(Checkbox, { id: showInfoPageId, name: "gs.dialog.pdfExport.checkbox.includePageExportInfo", text: intl.formatMessage({ id: "dialogs.export.pdf.includePageExportInfo" }), value: shouldShowInfoPage, onChange: setShouldShowInfoPage, labelSize: "normal" }) }))] }) }));
});
