"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFilesGallery = useFilesGallery;
const react_1 = require("react");
const components_1 = require("@gravity-ui/components");
const uikit_1 = require("@gravity-ui/uikit");
const gallery_1 = require("../../../i18n/gallery/index.js");
const constants_1 = require("./constants.js");
const helpers_1 = require("./helpers.js");
function useFilesGallery(customFiles, { download: getItemDownloladUrl, overrideItemProps, copyUrl: getItemCopyUrl, resolveCustomItem, } = {}) {
    const { openGallery } = (0, components_1.useGallery)();
    const toaster = (0, uikit_1.useToaster)();
    return {
        openFilesGallery: (0, react_1.useCallback)((event) => {
            if (!(event.target instanceof HTMLElement)) {
                return false;
            }
            if (event.target.tagName === 'IMG' && event.target.closest('a'))
                return false;
            // Opening the context menu or opening the link in the new tab
            if (event.ctrlKey || event.metaKey) {
                return false;
            }
            const buildItem = (link, type, element, baseProps) => {
                const galleryItemActions = [...(baseProps.actions ?? [])];
                const itemCopyUrl = getItemCopyUrl?.(link, type, element);
                if (itemCopyUrl) {
                    const handleLinkCopied = () => {
                        toaster.add({
                            theme: 'success',
                            name: 'g-md-editor-gallery-copy-link',
                            title: (0, gallery_1.i18n)('link_copied'),
                        });
                    };
                    galleryItemActions.push((0, components_1.getGalleryItemCopyLinkAction)({
                        copyUrl: itemCopyUrl,
                        onCopy: handleLinkCopied,
                    }));
                }
                const downloadUrl = getItemDownloladUrl?.(link, type, element);
                if (downloadUrl) {
                    galleryItemActions.push((0, components_1.getGalleryItemDownloadAction)({ downloadUrl }));
                }
                const galleryItemProps = {
                    ...baseProps,
                    url: link,
                    actions: galleryItemActions,
                };
                return {
                    ...galleryItemProps,
                    ...overrideItemProps?.(link, type, element, galleryItemProps),
                };
            };
            const targetFile = (0, helpers_1.buildLinkObject)(event.target);
            if (!targetFile || !targetFile.link)
                return false;
            const filesFromContent = [
                ...(event.currentTarget?.querySelectorAll('img,a') ?? []),
            ].reduce((result, element) => {
                const linkObj = (0, helpers_1.buildLinkObject)(element);
                if (linkObj?.link && !customFiles?.some((item) => item.url === linkObj.link)) {
                    const extension = linkObj.mimetype?.match(constants_1.extensionRegex)?.[0] ||
                        // skip regex on data URIs to avoid catastrophic backtracking
                        (/^data:/i.test(linkObj.link)
                            ? ''
                            : linkObj.link.match(constants_1.extensionRegex)?.[0]) ||
                        '';
                    if (linkObj.type === 'image' || constants_1.supportedExtensions.includes(extension)) {
                        const link = linkObj.link;
                        const name = linkObj.name || '';
                        const type = constants_1.supportedVideoExtensions.includes(extension)
                            ? 'video'
                            : 'image';
                        const baseProps = {
                            ...(type === 'video'
                                ? (0, components_1.getGalleryItemVideo)({ src: link, name })
                                : (0, components_1.getGalleryItemImage)({ src: link, name })),
                            url: link,
                            actions: undefined,
                        };
                        result.push(buildItem(link, type, element, baseProps));
                    }
                    else if (resolveCustomItem) {
                        const link = linkObj.link;
                        const baseProps = resolveCustomItem(link, 'file', element, {
                            name: linkObj.name,
                            mimetype: linkObj.mimetype,
                        });
                        if (baseProps) {
                            result.push(buildItem(link, 'file', element, baseProps));
                        }
                    }
                }
                return result;
            }, []);
            const files = [...(customFiles ?? []), ...filesFromContent];
            const initialItemIndex = files.findIndex((item) => item.url === targetFile.link);
            if (initialItemIndex !== -1) {
                event.preventDefault();
                openGallery(files, initialItemIndex);
                return true;
            }
            return false;
        }, [
            customFiles,
            getItemCopyUrl,
            getItemDownloladUrl,
            overrideItemProps,
            resolveCustomItem,
            toaster,
            openGallery,
        ]),
    };
}
//# sourceMappingURL=useFilesGallery.js.map
