"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileForm = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const uikit_1 = require("@gravity-ui/uikit");
const legacy_1 = require("@gravity-ui/uikit/legacy");
const classname_1 = require("../classname.js");
const forms_1 = require("../i18n/forms/index.js");
const lodash_1 = require("../lodash.js");
const handlers_1 = require("../utils/handlers.js");
const TextInput_1 = require("./TextInput.js");
const base_1 = tslib_1.__importDefault(require("./base/index.js"));
const components_1 = require("./components/index.js");
const b = (0, classname_1.cn)('file-form');
var TabId;
(function (TabId) {
    TabId["Attach"] = "attach";
    TabId["Link"] = "link";
})(TabId || (TabId = {}));
const FileForm = ({ className, autoFocus, onCancel, onSubmit, onAttach, loading, uploadHint, }) => {
    const [tabId, setTabId] = (0, react_1.useState)(() => (0, lodash_1.isFunction)(onAttach) ? TabId.Attach : TabId.Link);
    const [src, setSrc] = (0, react_1.useState)('');
    const [name, setName] = (0, react_1.useState)('');
    const shouldRenderTabs = (0, lodash_1.isFunction)(onAttach);
    (0, react_1.useLayoutEffect)(() => {
        if (!shouldRenderTabs && tabId === TabId.Attach) {
            setTabId(TabId.Link);
        }
    }, [shouldRenderTabs, tabId]);
    const handleSubmit = () => {
        onSubmit({
            src: src.trim(),
            name: name.trim(),
        });
    };
    const inputEnterKeyHandler = (0, handlers_1.enterKeyHandler)(handleSubmit);
    return ((0, jsx_runtime_1.jsxs)(base_1.default.Form, { className: b(null, [className]), children: [shouldRenderTabs && ((0, jsx_runtime_1.jsx)(legacy_1.Tabs, { activeTab: tabId, onSelectTab: setTabId, items: [
                    { id: TabId.Attach, title: (0, forms_1.i18n)('common_tab_attach') },
                    { id: TabId.Link, title: (0, forms_1.i18n)('common_tab_link') },
                ] })), tabId === TabId.Attach && onAttach && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(base_1.default.Layout, { children: uploadHint ?? (0, forms_1.i18n)('file_upload_help') }), (0, jsx_runtime_1.jsx)(base_1.default.Footer, { onCancel: onCancel, children: (0, jsx_runtime_1.jsx)(components_1.ButtonAttach, { multiple: true, buttonProps: {
                                size: 's',
                                view: 'action',
                                loading,
                            }, onUpdate: onAttach, children: (0, forms_1.i18n)('common_action_upload') }) })] })), tabId === TabId.Link && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(base_1.default.Layout, { children: [(0, jsx_runtime_1.jsx)(base_1.default.Row, { label: (0, forms_1.i18n)('common_link'), help: (0, forms_1.i18n)('file_link_help'), control: (0, jsx_runtime_1.jsx)(TextInput_1.TextInputFixed, { size: "s", view: "normal", value: src, onUpdate: setSrc, placeholder: "https://", autoFocus: autoFocus, className: b('input', { type: 'link' }), onKeyPress: inputEnterKeyHandler }) }), (0, jsx_runtime_1.jsx)(base_1.default.Row, { label: (0, forms_1.i18n)('file_name'), control: (0, jsx_runtime_1.jsx)(uikit_1.TextInput, { size: "s", view: "normal", value: name, onUpdate: setName, className: b('input', { type: 'name' }), onKeyPress: inputEnterKeyHandler }) })] }), (0, jsx_runtime_1.jsx)(base_1.default.Footer, { onCancel: onCancel, onSubmit: handleSubmit })] }))] }));
};
exports.FileForm = FileForm;
//# sourceMappingURL=FileForm.js.map
