"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const ErrorService_1 = require("../../../../services/ErrorService");
const constants_1 = require("../../../constants");
const useGenerateLocalFileURL_1 = require("../../../hooks/useGenerateLocalFileURL");
const MimeTypeRegistry_1 = require("../../../services/MimeTypeRegistry");
const generateAttrLinkNewTab_1 = require("../../../utils/generateAttrLinkNewTab");
const useLogic = ({ fileId, file, fileUrl, generateDownloadLink, onDelete, onView, fileType, }) => {
    const { generateUrl } = (0, useGenerateLocalFileURL_1.useGenerateLocalFileURL)();
    const isPdfFile = MimeTypeRegistry_1.MimeTypeRegistry.getInstance()
        .getExtensionsByMimeType((file?.type || fileType))
        ?.includes('pdf') || false;
    const isImageFile = fileType.includes('image/');
    const isOtherFile = !isImageFile && !isPdfFile;
    const getDownloadLink = () => {
        if (generateDownloadLink) {
            return generateDownloadLink(fileId, file, fileUrl);
        }
        if (fileUrl) {
            return fileUrl;
        }
        if (file) {
            return generateUrl(file);
        }
        return '';
    };
    const downloadLink = getDownloadLink();
    const handleDelete = () => onDelete(fileId);
    const handleView = () => onView?.(fileId, file, fileUrl);
    const checkIsAcceptedExtension = (extension) => {
        if (!extension) {
            return false;
        }
        if (Array.isArray(extension)) {
            return extension.some((ext) => constants_1.EXTENSIONS_IN_NEW_TAB.includes(ext));
        }
        return constants_1.EXTENSIONS_IN_NEW_TAB.includes(extension);
    };
    const getPropsViewButton = () => {
        if (onView) {
            return { onClick: handleView };
        }
        const extensions = MimeTypeRegistry_1.MimeTypeRegistry.getInstance().getExtensionsByMimeType((file?.type || fileType));
        if (checkIsAcceptedExtension(extensions)) {
            return (0, generateAttrLinkNewTab_1.generateAttrLinkNewTab)(downloadLink);
        }
        return {
            onClick: () => {
                // biome-ignore lint/suspicious/noConsole: Ошибка для интеграторов
                console.warn(constants_1.VIEW_NEW_TAB_ERROR_MESSAGE);
                ErrorService_1.ErrorService.getInstance().captureCustomError('PreviewFileUploader', `FileId: ${fileId}: ${constants_1.VIEW_NEW_TAB_ERROR_MESSAGE}`);
            },
        };
    };
    return {
        deleteButtonProps: {
            onClick: handleDelete,
        },
        downloadButtonProps: {
            href: downloadLink,
        },
        viewButtonProps: getPropsViewButton(),
        isImageFile,
        isPdfFile,
        /**
         * Формат файла отличающийся от pdf и изображения
         */
        isOtherFile,
    };
};
exports.useLogic = useLogic;
