import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2019-2026 GoodData Corporation
import { useCallback, useRef, useState } from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { Message } from "@gooddata/sdk-ui-kit";
import { AUTOMATION_ATTACHMENTS_GROUP_LABEL_ID } from "../../../../../constants/automations.js";
import { AttachmentsList } from "./AttachmentsList.js";
import { AttachmentsSelect } from "./AttachmentsSelect.js";
import { AttachmentsWrapper } from "./AttachmentsWrapper.js";
const SUPPORTED_DASHBOARD_ATTACHMENTS = ["PDF", "PDF_SLIDES", "PPTX", "XLSX"];
export function DashboardAttachments({ dashboardFilters, isCrossFiltering, onDashboardAttachmentsChange, selectedAttachments, xlsxSettings, onXlsxSettingsChange, defaultPdfPageSize, exportTemplates, slidesTemplateIds, onSlidesTemplateIdChange, }) {
    const intl = useIntl();
    const attachmentListRef = useRef(null);
    const addButtonRef = useRef(null);
    const [announcement, setAnnouncement] = useState("");
    const handleDashboardAttachmentSelectionSave = (formats) => {
        onDashboardAttachmentsChange(formats, dashboardFilters);
    };
    const focusAttachmentGroup = useCallback(() => {
        requestAnimationFrame(() => {
            if (attachmentListRef.current) {
                attachmentListRef.current.tabIndex = 0;
                attachmentListRef.current.focus();
            }
        });
    }, []);
    const makeAttachmentGroupUnfocusable = useCallback(() => {
        requestAnimationFrame(() => {
            if (attachmentListRef.current) {
                attachmentListRef.current.removeAttribute("tabindex");
            }
        });
    }, []);
    const handleDelete = (attachment) => {
        const newAttachments = selectedAttachments.filter((att) => att !== attachment);
        onDashboardAttachmentsChange(newAttachments, dashboardFilters);
        setTimeout(() => {
            setAnnouncement(intl.formatMessage({ id: "dialogs.schedule.management.attachments.removed" }, { format: attachment }));
        });
        // Focus management: if items remain, focus the group; otherwise focus add button
        if (newAttachments.length > 0) {
            focusAttachmentGroup();
        }
        else {
            requestAnimationFrame(() => {
                addButtonRef.current?.focus();
            });
        }
    };
    const handleChange = (attachments) => {
        const formats = attachments
            .filter((attachment) => attachment.selected && SUPPORTED_DASHBOARD_ATTACHMENTS.includes(attachment.type))
            .map((attachment) => attachment.type);
        handleDashboardAttachmentSelectionSave(formats);
        // Focus add button after state update causes remount (returnFocusTo ref becomes stale)
        requestAnimationFrame(() => {
            addButtonRef.current?.focus();
        });
        if (attachmentListRef.current) {
            setTimeout(() => {
                attachmentListRef.current?.scrollIntoView({ behavior: "smooth", block: "nearest" });
            }, 100);
        }
    };
    return (_jsxs(_Fragment, { children: [
            _jsx(AttachmentsWrapper, { children: _jsxs("div", { className: "gd-attachment-list", role: "group", "aria-labelledby": AUTOMATION_ATTACHMENTS_GROUP_LABEL_ID, ref: attachmentListRef, onBlur: makeAttachmentGroupUnfocusable, children: [
                        _jsx(AttachmentsList, { attachments: selectedAttachments, onDelete: handleDelete, xlsxSettings: xlsxSettings, onXlsxSettingsChange: onXlsxSettingsChange, defaultPdfPageSize: defaultPdfPageSize, mode: "dashboard", exportTemplates: exportTemplates, slidesTemplateIds: slidesTemplateIds, onSlidesTemplateIdChange: onSlidesTemplateIdChange }), _jsx(AttachmentsSelect, { attachments: SUPPORTED_DASHBOARD_ATTACHMENTS.map((format) => ({
                                type: format,
                                selected: selectedAttachments.includes(format),
                            })), onChange: handleChange, mode: "dashboard", onAddButtonRef: (ref) => {
                                addButtonRef.current = ref;
                            } }), isCrossFiltering && selectedAttachments.length > 0 ? (_jsx(Message, { type: "progress", className: "gd-attachment-list-message", children: _jsx(FormattedMessage, { id: "dialogs.schedule.management.attachments.message", values: {
                                    strong: (chunks) => _jsx("strong", { children: chunks }),
                                } }) })) : null] }) }, selectedAttachments.join()), _jsx("div", { className: "sr-only", "aria-live": "polite", "aria-atomic": "true", role: "status", children: announcement })
        ] }));
}
