"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFilesGallery = useFilesGallery;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(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");
function useFilesGallery(customFiles, { download: getItemDownloladUrl, overrideItemProps, copyUrl: getItemCopyUrl, } = {}) {
    const { openGallery } = (0, components_1.useGallery)();
    const toaster = (0, uikit_1.useToaster)();
    return {
        openFilesGallery: React.useCallback((event) => {
            if (!(event.target instanceof HTMLElement)) {
                return false;
            }
            let fileLink = '';
            if (event.target.tagName === 'IMG' && !event.target.closest('a')) {
                fileLink = event.target.getAttribute('src') ?? '';
            }
            else if (event.target.tagName === 'A') {
                fileLink = event.target.getAttribute('href') ?? '';
            }
            if (!fileLink) {
                return false;
            }
            const filesFromContent = [
                ...(event.currentTarget?.querySelectorAll('img,a') ?? []),
            ].reduce((result, element) => {
                const isImage = element.tagName === 'IMG';
                const link = isImage
                    ? element.getAttribute('src')
                    : element.getAttribute('href');
                if (link && !customFiles?.some((item) => item.url === link)) {
                    const extension = link.match(constants_1.extensionRegex)?.[0] || '';
                    if (isImage || constants_1.supportedExtensions.includes(extension)) {
                        const name = (isImage
                            ? element.getAttribute('alt')
                            : element.getAttribute('title')) || '';
                        const filesGalleryItemType = constants_1.supportedVideoExtensions.includes(extension) ? 'video' : 'image';
                        const galleryItemActions = [];
                        const itemCopyUrl = getItemCopyUrl?.(link, filesGalleryItemType, 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, filesGalleryItemType, element);
                        if (downloadUrl) {
                            galleryItemActions.push((0, components_1.getGalleryItemDownloadAction)({ downloadUrl }));
                        }
                        const galleryItemProps = {
                            ...(filesGalleryItemType === 'video'
                                ? (0, components_1.getGalleryItemVideo)({ src: link, name: name })
                                : (0, components_1.getGalleryItemImage)({ src: link, name: name })),
                            url: link,
                            actions: galleryItemActions,
                        };
                        result.push({
                            ...galleryItemProps,
                            ...overrideItemProps?.(link, filesGalleryItemType, element, galleryItemProps),
                        });
                    }
                }
                return result;
            }, []);
            const files = [...(customFiles ?? []), ...filesFromContent];
            const initialItemIndex = files.findIndex((item) => item.url === fileLink);
            if (initialItemIndex !== -1) {
                event.preventDefault();
                openGallery(files, initialItemIndex);
                return true;
            }
            return false;
        }, [
            customFiles,
            getItemCopyUrl,
            getItemDownloladUrl,
            overrideItemProps,
            toaster,
            openGallery,
        ]),
    };
}
//# sourceMappingURL=useFilesGallery.js.map
