"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_redux_1 = require("react-redux");
const react_router_dom_1 = require("react-router-dom");
const antd_1 = require("antd");
const ACTIONS = __importStar(require("@/actions/applications/detail/settings/builder/component"));
const index_1 = require("@/components/index");
const system_1 = require("@/pages/system");
const BasicInfoPanel_1 = __importDefault(require("./BasicInfoPanel"));
const mapStateToProps = (store) => ({
    loading: store.applications.detail.settings.builder.components.loading,
    editorState: store.applications.detail.settings.builder.components.editor,
    categories: store.applications.detail.settings.builder.components.categories,
});
const mapDispatchToProps = {
    closeDrawer: ACTIONS.updateEditorVisible,
    save: ACTIONS.saveCategory,
    fetchCategories: ACTIONS.fetchCategories,
};
const Editor = (props) => {
    var _a, _b, _c;
    const { applicationId } = (0, react_router_dom_1.useParams)();
    const [category, setCategory] = (0, react_1.useState)((_b = (_a = props.editorState) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.category);
    const { loading, editorState, categories, closeDrawer, save, fetchCategories } = props;
    const { status, data } = editorState;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, setting, category: i18n } = locale.business;
    (0, react_1.useEffect)(() => {
        if (!status) {
            setCategory(undefined);
            fetchCategories(applicationId);
        }
    }, [status]);
    (0, react_1.useEffect)(() => {
        var _a, _b;
        setCategory((_b = (_a = props.editorState) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.category);
    }, [props.editorState]);
    const checker = (params) => {
        const { name, categoryName, groupName } = params || {};
        if (!name) {
            antd_1.message.warning(i18n.nameEmptyTips);
            return false;
        }
        if (!categoryName) {
            antd_1.message.warning(i18n.levelOneEmptyTips);
            return false;
        }
        if (!groupName) {
            antd_1.message.warning(i18n.levelTwoEmptyTips);
            return false;
        }
        return true;
    };
    const onSave = () => {
        if (category && checker(category)) {
            save({
                applicationId,
                type: 'component',
                setting: [
                    {
                        category,
                        id: editorState === null || editorState === void 0 ? void 0 : editorState.data.id,
                        name: (editorState === null || editorState === void 0 ? void 0 : editorState.data.name) || '',
                        status: false,
                    },
                ],
            });
        }
    };
    return (react_1.default.createElement(index_1.OperationDrawer, { maskClosable: false, open: status, title: ((_c = data === null || data === void 0 ? void 0 : data.category) === null || _c === void 0 ? void 0 : _c.categoryName) ? global.edit : global.add + setting.componentBar, onClose: () => closeDrawer(false, null), width: 480, destroyOnClose: true, actions: react_1.default.createElement(antd_1.Button, { type: "primary", onClick: onSave }, global.apply) },
        react_1.default.createElement(antd_1.Spin, { spinning: loading },
            react_1.default.createElement(BasicInfoPanel_1.default, { key: status, category: category, categories: categories, onChange: setCategory }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Editor);
