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 { useIntl } from "react-intl";
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_WIDGET_ATTACHMENTS = [
    "PNG",
    "PPTX",
    "PDF",
    "PDF_TABULAR",
    "XLSX",
    "CSV",
    "CSV_RAW",
];
export function WidgetAttachments({ selectedAttachments, onWidgetAttachmentsChange, xlsxSettings, onXlsxSettingsChange, pdfSettings, onPdfSettingsChange, csvSettings, onCsvSettingsChange, csvRawSettings, onCsvRawSettingsChange, isCsvSettingsEnabled, defaultPdfPageSize, exportTemplates, slidesTemplateIds, onSlidesTemplateIdChange, }) {
    const intl = useIntl();
    const attachmentListRef = useRef(null);
    const addButtonRef = useRef(null);
    const [announcement, setAnnouncement] = useState("");
    const handleWidgetAttachmentSelectionSave = (formats) => {
        onWidgetAttachmentsChange(formats);
    };
    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);
        onWidgetAttachmentsChange(newAttachments);
        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_WIDGET_ATTACHMENTS.includes(attachment.type))
            .map((attachment) => attachment.type);
        handleWidgetAttachmentSelectionSave(formats);
        // Focus add button after state update causes remount (returnFocusTo ref becomes stale)
        requestAnimationFrame(() => {
            addButtonRef.current?.focus();
        });
        // Scroll the attachment list into view after change
        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, pdfSettings: pdfSettings, onPdfSettingsChange: onPdfSettingsChange, csvSettings: csvSettings, onCsvSettingsChange: onCsvSettingsChange, csvRawSettings: csvRawSettings, onCsvRawSettingsChange: onCsvRawSettingsChange, isCsvSettingsEnabled: isCsvSettingsEnabled, defaultPdfPageSize: defaultPdfPageSize, mode: "widget", exportTemplates: exportTemplates, slidesTemplateIds: slidesTemplateIds, onSlidesTemplateIdChange: onSlidesTemplateIdChange }), _jsx(AttachmentsSelect, { attachments: SUPPORTED_WIDGET_ATTACHMENTS.map((format) => ({
                                type: format,
                                selected: selectedAttachments.includes(format),
                            })), onChange: handleChange, mode: "widget", onAddButtonRef: (ref) => {
                                addButtonRef.current = ref;
                            } })
                    ] }) }, selectedAttachments.join()), _jsx("div", { className: "sr-only", "aria-live": "polite", "aria-atomic": "true", role: "status", children: announcement })
        ] }));
}
