"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_codemirror2_1 = require("react-codemirror2");
const react_redux_1 = require("react-redux");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const ACTIONS = __importStar(require("@/actions/applications/detail/file/functions"));
const index_1 = require("@/components/index");
const system_1 = require("@/pages/system");
require("./style.css");
require('codemirror/mode/htmlmixed/htmlmixed');
const FN_TYPE = ['sync'];
const { Option } = antd_1.Select;
const CodeBox = styled_components_1.default.div `
  min-height: 300px;
  & > .CodeMirror {
    height: 300px;
  }
`;
const mapStateToProps = (store) => ({
    pageInfo: store.applications.detail.file.functions.pageInfo,
    type: store.applications.detail.file.functions.drawerType,
    visible: store.applications.detail.file.functions.drawerVisible,
    func: store.applications.detail.file.functions.editFunc,
});
const mapDispatchToProps = {
    closeDrawer: ACTIONS.openEditDrawer,
    fetchList: ACTIONS.fetchList,
    saveFunction: ACTIONS.saveFunction,
};
function EditDrawer(props) {
    const { applicationId, folderId, pageInfo, type, visible, func, closeDrawer, fetchList, saveFunction, } = props;
    const [name, setName] = (0, react_1.useState)('');
    const [code, setCode] = (0, react_1.useState)('');
    const [fnType, setFnType] = (0, react_1.useState)(0);
    const viewMode = !!type && type === 'view';
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, function: functionI18n } = locale.business;
    (0, react_1.useEffect)(() => {
        var _a, _b, _c, _d;
        if (visible) {
            // get name & set default when edit
            const funcName = (func === null || func === void 0 ? void 0 : func.name) || undefined;
            if (funcName)
                setName(funcName);
            // get async type & set default when edit
            const { content } = func || {};
            const { schemas } = content || {};
            const type = schemas ? (_b = (_a = schemas[0]) === null || _a === void 0 ? void 0 : _a.props) === null || _b === void 0 ? void 0 : _b.async : undefined;
            if (typeof type !== 'undefined')
                setFnType(type ? 1 : 0);
            // get code & set default when edit
            const funcCode = schemas ? (_d = (_c = schemas[0]) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.code : undefined;
            if (funcCode)
                setCode(funcCode);
        }
    }, [visible, func]);
    const handleClose = (0, react_1.useCallback)(() => {
        setName('');
        setCode('');
        setFnType(0);
        setTimeout(() => closeDrawer(false), 50);
    }, [closeDrawer]);
    const handleApply = (0, react_1.useCallback)(() => {
        const id = (func === null || func === void 0 ? void 0 : func.id) || '';
        const contentId = (func === null || func === void 0 ? void 0 : func.contentId) || '';
        const newSchemas = [
            {
                name,
                type: 'javascript.function',
                props: {
                    async: !!fnType,
                    code,
                },
            },
        ];
        if (applicationId) {
            saveFunction({
                applicationId,
                folderId,
                id,
                name,
                content: {
                    id: contentId,
                    schemas: newSchemas,
                    relation: {},
                },
                type: 'function',
            }, () => {
                handleClose();
                fetchList({ applicationId, folderId, page: pageInfo.page, size: pageInfo.size });
            });
        }
    }, [applicationId, name, code, fnType, saveFunction, handleClose]);
    const codeMirrorOptions = {
        lineNumbers: true,
        mode: { name: 'javascript', json: true },
        lineWrapping: true,
        readOnly: viewMode,
    };
    return (react_1.default.createElement(index_1.OperationDrawer, { destroyOnClose: true, width: 550, open: visible, title: (func === null || func === void 0 ? void 0 : func.id) ? global.edit : global.add, actions: react_1.default.createElement(antd_1.Button, { type: "primary", disabled: viewMode, onClick: handleApply }, global.apply), onClose: handleClose },
        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.nameLabel),
                react_1.default.createElement(antd_1.Input, { disabled: func.id || viewMode, placeholder: "name must contain at least 5 characters", value: name, onChange: (e) => setName(e.target.value) })),
            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: func.id || viewMode, placeholder: "type", value: fnType, onChange: setFnType, style: { width: 160 } }, FN_TYPE.map((item, idx) => (react_1.default.createElement(Option, { key: item, value: idx }, item))))),
            react_1.default.createElement(index_1.Field, null,
                react_1.default.createElement(index_1.Label, null, functionI18n.name),
                react_1.default.createElement(CodeBox, null,
                    react_1.default.createElement(react_codemirror2_1.Controlled, { key: func.id, options: codeMirrorOptions, value: code, onBeforeChange: (_editor, _data, value) => {
                            setCode(value);
                        } }))))));
}
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(EditDrawer);
