"use strict";
exports.__esModule = true;
exports.FileCard = void 0;
var tslib_1 = require("tslib");
// @ts-nocheck
require("./FileCard.css");
var react_1 = (0, tslib_1.__importStar)(require("react"));
var html_react_parser_1 = (0, tslib_1.__importDefault)(require("html-react-parser"));
var IconTrash_1 = require("../../icons/IconTrash/IconTrash");
var MixFocus_1 = require("../../mixs/MixFocus/MixFocus");
var bem_1 = require("../../utils/bem");
var File_1 = require("../File/File");
var Modal_1 = require("../Modal/Modal");
var Typography_1 = require("../Typography/Typography");
var FileCardForm_1 = require("./FileCardForm");
var cnFileCard = (0, bem_1.cn)('FileCard');
var FileCard = function (_a, ref) {
    var _b;
    var title = _a.title, extension = _a.extension, size = _a.size, date = _a.date, url = _a.url, onSubmit = _a.onSubmit, popup = _a.popup, view = _a.view, removeHandler = _a.removeHandler, recaptchaKey = _a.recaptchaKey;
    var _c = (0, tslib_1.__read)((0, react_1.useState)(false), 2), isModalOpen = _c[0], setIsModalOpen = _c[1];
    var _d = (0, tslib_1.__read)((0, react_1.useState)(false), 2), successForm = _d[0], setSuccessForm = _d[1];
    var bytesToSize = function (bytes) {
        var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
        if (!bytes)
            return '0 Byte';
        var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
        return Math.round(bytes / Math.pow(1024, i), 2) + sizes[i];
    };
    var handlerClick = function () {
        if (popup) {
            document.body.style.overflow = 'unset';
            setIsModalOpen(true);
        }
        else if (url)
            window.open(url, '_blank');
    };
    var onSubmitHandler = function (values) { return (0, tslib_1.__awaiter)(void 0, void 0, void 0, function () {
        return (0, tslib_1.__generator)(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, onSubmit(values).then(function () {
                        setSuccessForm(true);
                        window.open(url, '_blank');
                    })];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); };
    return (react_1["default"].createElement(react_1["default"].Fragment, null,
        react_1["default"].createElement("button", { type: "button", className: cnFileCard({ clicked: !!url, view: view }, [(0, MixFocus_1.cnMixFocus)()]), onClick: handlerClick },
            react_1["default"].createElement("div", { className: cnFileCard('Icon') },
                react_1["default"].createElement(File_1.File, { extension: extension })),
            react_1["default"].createElement("div", { className: cnFileCard('Inner') },
                react_1["default"].createElement(Typography_1.Typography.Text, { weight: "bold", className: cnFileCard('Title') }, title),
                react_1["default"].createElement(Typography_1.Typography.Text, { size: "s", className: cnFileCard('Text') }, (_b = bytesToSize(size)) !== null && _b !== void 0 ? _b : '',
                    "\u00A0\u00A0",
                    date ? react_1["default"].createElement(react_1["default"].Fragment, null,
                        "Last updated ",
                        date) : null)),
            removeHandler && (react_1["default"].createElement("div", { className: cnFileCard('ActionBtn'), onClick: removeHandler },
                react_1["default"].createElement("span", null,
                    react_1["default"].createElement(IconTrash_1.IconTrash, { size: "s" }))))),
        popup && (react_1["default"].createElement(Modal_1.Modal, { hasOverlay: true, view: "form", inverted: successForm, onOpen: function () {
                document.body.style.overflow = 'hidden';
            }, onClose: function () {
                document.body.style.overflow = 'unset';
            }, isOpen: isModalOpen, onOverlayClick: function () {
                document.body.style.overflow = 'unset';
                setIsModalOpen(false);
                setSuccessForm(false);
            }, onEsc: function () {
                setIsModalOpen(false);
                setSuccessForm(false);
                document.body.style.overflow = 'unset';
            }, showCloseButton: true },
            react_1["default"].createElement(Typography_1.Typography.Title, { size: "md", className: cnFileCard('Modal-Title') }, popup.title),
            react_1["default"].createElement(Typography_1.Typography.Text, null, (0, html_react_parser_1["default"])(popup.text)),
            react_1["default"].createElement(FileCardForm_1.FileCardForm, { url: url, onSubmit: onSubmitHandler, type: popup.formType, dictionaries: popup.dictionary, recaptchaKey: recaptchaKey }),
            successForm && (react_1["default"].createElement("div", { className: cnFileCard('ModalSuccess') },
                react_1["default"].createElement(Typography_1.Typography.Title, { size: "lg", className: cnFileCard('Title') }, popup.dictionary.companySiteFormDownloadFile
                    .companySiteFormDownloadFileSuccessTitle),
                react_1["default"].createElement(Typography_1.Typography.Text, { className: cnFileCard('SubTitle') }, popup.dictionary.companySiteFormDownloadFile
                    .companySiteFormDownloadFileSuccessText)))))));
};
exports.FileCard = FileCard;
