import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useLayoutEffect, useState } from 'react';
import { TextInput } from '@gravity-ui/uikit';
import { Tabs } from '@gravity-ui/uikit/legacy';
import { cn } from "../classname.js";
import { i18n } from "../i18n/forms/index.js";
import { isFunction } from "../lodash.js";
import { enterKeyHandler } from "../utils/handlers.js";
import { TextInputFixed } from "./TextInput.js";
import Form from "./base/index.js";
import { ButtonAttach } from "./components/index.js";
const b = cn('file-form');
var TabId;
(function (TabId) {
    TabId["Attach"] = "attach";
    TabId["Link"] = "link";
})(TabId || (TabId = {}));
export const FileForm = ({ className, autoFocus, onCancel, onSubmit, onAttach, loading, uploadHint, }) => {
    const [tabId, setTabId] = useState(() => isFunction(onAttach) ? TabId.Attach : TabId.Link);
    const [src, setSrc] = useState('');
    const [name, setName] = useState('');
    const shouldRenderTabs = isFunction(onAttach);
    useLayoutEffect(() => {
        if (!shouldRenderTabs && tabId === TabId.Attach) {
            setTabId(TabId.Link);
        }
    }, [shouldRenderTabs, tabId]);
    const handleSubmit = () => {
        onSubmit({
            src: src.trim(),
            name: name.trim(),
        });
    };
    const inputEnterKeyHandler = enterKeyHandler(handleSubmit);
    return (_jsxs(Form.Form, { className: b(null, [className]), children: [shouldRenderTabs && (_jsx(Tabs, { activeTab: tabId, onSelectTab: setTabId, items: [
                    { id: TabId.Attach, title: i18n('common_tab_attach') },
                    { id: TabId.Link, title: i18n('common_tab_link') },
                ] })), tabId === TabId.Attach && onAttach && (_jsxs(_Fragment, { children: [_jsx(Form.Layout, { children: uploadHint ?? i18n('file_upload_help') }), _jsx(Form.Footer, { onCancel: onCancel, children: _jsx(ButtonAttach, { multiple: true, buttonProps: {
                                size: 's',
                                view: 'action',
                                loading,
                            }, onUpdate: onAttach, children: i18n('common_action_upload') }) })] })), tabId === TabId.Link && (_jsxs(_Fragment, { children: [_jsxs(Form.Layout, { children: [_jsx(Form.Row, { label: i18n('common_link'), help: i18n('file_link_help'), control: _jsx(TextInputFixed, { size: "s", view: "normal", value: src, onUpdate: setSrc, placeholder: "https://", autoFocus: autoFocus, className: b('input', { type: 'link' }), onKeyPress: inputEnterKeyHandler }) }), _jsx(Form.Row, { label: i18n('file_name'), control: _jsx(TextInput, { size: "s", view: "normal", value: name, onUpdate: setName, className: b('input', { type: 'name' }), onKeyPress: inputEnterKeyHandler }) })] }), _jsx(Form.Footer, { onCancel: onCancel, onSubmit: handleSubmit })] }))] }));
};
//# sourceMappingURL=FileForm.js.map
