import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2025 GoodData Corporation
import { useCallback, useState } from "react";
import cx from "classnames";
import { FormattedMessage, useIntl } from "react-intl";
import { Button, ContentDivider, Dropdown, isEscapeKey, isSpaceKey, } from "@gooddata/sdk-ui-kit";
const DROPDOWN_ALIGN_POINTS = [
    {
        align: "bc tc",
        offset: { x: 0, y: 0 },
    },
    {
        align: "tc bc",
        offset: { x: 0, y: 0 },
    },
];
function AttachmentItem({ format, checked, onChange, className, disabled, }) {
    const intl = useIntl();
    const handleKeyDown = (e) => {
        if (disabled) {
            return;
        }
        if (isSpaceKey(e)) {
            e.preventDefault();
            onChange();
        }
    };
    return (_jsxs("label", { className: "gd-notifications-channels-attachment-checkbox input-checkbox-label", children: [
            _jsx("input", { type: "checkbox", className: "input-checkbox", disabled: disabled, checked: checked, onChange: onChange, onKeyDown: handleKeyDown, "aria-label": intl.formatMessage({ id: "dialogs.schedule.management.attachments.attachment" }, { format: format }) }), _jsx("span", { className: "input-label-text" }), _jsx("div", { className: cx("gd-attachment-item", `s-attachment-item-${format.toLowerCase()}`, className), children: _jsx("span", { className: "gd-attachment-item-format", children: format }) })
        ] }));
}
export function AttachmentDashboard({ pdfSelected, disabled, onSelectionChange, }) {
    return (_jsx(AttachmentItem, { format: "PDF", disabled: disabled, checked: pdfSelected, onChange: onSelectionChange }));
}
export function AttachmentWidgets({ csvSelected, xlsxSelected, settings, onSelectionChange, onSettingsChange, closeOnParentScroll, overlayPositionType, }) {
    const intl = useIntl();
    const [mergeHeaders, setMergeHeaders] = useState(settings.mergeHeaders);
    const handleInputKeyDown = useCallback((e) => {
        if (isSpaceKey(e)) {
            e.preventDefault();
            setMergeHeaders(!mergeHeaders);
        }
    }, [setMergeHeaders, mergeHeaders]);
    return (_jsxs(_Fragment, { children: [
            _jsx(AttachmentItem, { format: "CSV", checked: csvSelected, onChange: () => onSelectionChange("CSV") }), _jsx(AttachmentItem, { format: "XLSX", checked: xlsxSelected, onChange: () => onSelectionChange("XLSX"), className: "gd-attachment-item-format-with-configuration" }), _jsx(Dropdown, { closeOnParentScroll: closeOnParentScroll, overlayPositionType: overlayPositionType, alignPoints: DROPDOWN_ALIGN_POINTS, autofocusOnOpen: true, renderButton: ({ toggleDropdown, buttonRef }) => (_jsx(Button, { className: "gd-attachment-item-configuration gd-icon-settings", onClick: toggleDropdown, accessibilityConfig: {
                        ariaLabel: intl.formatMessage({
                            id: "dialogs.schedule.management.attachments.xlsx.settings",
                        }),
                    }, ref: buttonRef })), renderBody: ({ closeDropdown }) => (_jsxs("div", { className: "gd-attachment-settings-dropdown", onKeyDown: (e) => {
                        if (isEscapeKey(e)) {
                            e.stopPropagation();
                            closeDropdown();
                        }
                    }, children: [
                        _jsxs("div", { className: "gd-list-title", children: [
                                _jsx(FormattedMessage, { id: "dialogs.schedule.management.attachments.xlsx.settings" }), _jsx("div", { className: "gd-close-button", children: _jsx(Button, { className: "gd-button-link gd-button-icon-only gd-icon-cross s-dialog-close-button", value: "", onClick: closeDropdown, accessibilityConfig: {
                                            ariaLabel: intl.formatMessage({ id: "close" }),
                                        } }) })
                            ] }), _jsx("div", { className: "gd-attachment-settings-dropdown-content", children: _jsxs("label", { className: "input-checkbox-label", children: [
                                    _jsx("input", { type: "checkbox", className: "input-checkbox", onChange: () => setMergeHeaders(!mergeHeaders), onKeyDown: handleInputKeyDown, checked: mergeHeaders }), _jsx("span", { className: "input-label-text", children: _jsx(FormattedMessage, { id: "dialogs.schedule.management.attachments.cells.merged" }) })
                                ] }) }), _jsx(ContentDivider, { className: "gd-divider-without-margin" }), _jsxs("div", { className: "gd-attachment-settings-dropdown-footer", children: [
                                _jsx(Button, { value: intl.formatMessage({ id: "cancel" }), className: "gd-button-secondary", onClick: closeDropdown }), _jsx(Button, { value: intl.formatMessage({ id: "save" }), className: "gd-button-action", onClick: () => {
                                        onSettingsChange({ mergeHeaders });
                                        closeDropdown();
                                    }, disabled: mergeHeaders === settings.mergeHeaders })
                            ] })
                    ] })) })] }));
}
//# sourceMappingURL=AttachmentItems.js.map