"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_router_dom_1 = require("react-router-dom");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const index_1 = require("@/components/index");
const index_2 = require("@/constants/index");
const system_1 = require("@/pages/system");
const index_3 = require("@/utils/index");
const { Option } = antd_1.Select;
const EditDrawer = (props) => {
    var _a;
    const { drawerOpen, saveLoading, pageInfo, editFile, updateEditFile, saveFile, fetchFileList, closeDrawer, } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, file, application } = locale.business;
    // url search params
    const { applicationId, folderId } = (0, index_3.getLocationIfo)((0, react_router_dom_1.useLocation)());
    const pathnameTag = ((_a = editFile === null || editFile === void 0 ? void 0 : editFile.tags) === null || _a === void 0 ? void 0 : _a.find((item) => item === null || item === void 0 ? void 0 : item.pathname)) || {};
    const handleSave = () => {
        if (!editFile.type) {
            antd_1.message.warning(application.typeInvalid);
            return;
        }
        if (!editFile.name) {
            antd_1.message.warning(application.nameInvalid);
            return;
        }
        if ((editFile === null || editFile === void 0 ? void 0 : editFile.name) && editFile.name.length < 5) {
            antd_1.message.warning(application.nameLengthInvalid);
            return;
        }
        if (applicationId && folderId) {
            saveFile({ applicationId, folderId }, () => {
                // close drawer
                closeDrawer(false);
                // refresh file list
                fetchFileList({
                    applicationId,
                    id: folderId,
                    page: pageInfo.page,
                    size: pageInfo.size,
                });
            });
        }
    };
    return (react_1.default.createElement(index_1.OperationDrawer, { destroyOnClose: true, width: 480, title: (editFile === null || editFile === void 0 ? void 0 : editFile.id) ? global.edit : global.add, open: drawerOpen, onClose: () => {
            closeDrawer(false);
        }, actions: react_1.default.createElement(antd_1.Button, { type: "primary", onClick: handleSave },
            global.apply,
            saveLoading && react_1.default.createElement(icons_1.SyncOutlined, { spin: true, style: { color: '#fff' } })) },
        react_1.default.createElement(index_1.Group, null,
            react_1.default.createElement(index_1.Field, null,
                react_1.default.createElement(index_1.Label, null, global.type),
                react_1.default.createElement(antd_1.Select, { disabled: !!(editFile === null || editFile === void 0 ? void 0 : editFile.id), value: editFile === null || editFile === void 0 ? void 0 : editFile.type, onSelect: (value) => {
                        updateEditFile('type', value);
                    }, style: { width: 150 } }, index_2.FileTypes.map((item) => (react_1.default.createElement(Option, { value: item.type, key: item.type }, file[item.type]))))),
            react_1.default.createElement(index_1.Field, null,
                react_1.default.createElement(index_1.Label, null, global.nameLabel),
                react_1.default.createElement(antd_1.Input, { value: editFile === null || editFile === void 0 ? void 0 : editFile.name, placeholder: file.nameLabel, onChange: (e) => updateEditFile('name', e.target.value) })),
            (editFile === null || editFile === void 0 ? void 0 : editFile.type) === index_2.FileType.page && (react_1.default.createElement(index_1.Field, null,
                react_1.default.createElement(index_1.Label, null, file.pathname),
                react_1.default.createElement(antd_1.Input, { value: pathnameTag === null || pathnameTag === void 0 ? void 0 : pathnameTag.pathname, placeholder: file.pathname, onChange: (e) => {
                        updateEditFile('tags', [{ pathname: e.target.value }]);
                    } }))))));
};
exports.default = EditDrawer;
