"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 });
exports.ModalTypeEnum = void 0;
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const CONDITION_ACTIONS = __importStar(require("@/actions/applications/detail/file/conditions"));
const FUNCTION_ACTIONS = __importStar(require("@/actions/applications/detail/file/functions"));
const VARIABLE_ACTIONS = __importStar(require("@/actions/applications/detail/file/variables"));
const HEADER_ACTIONS = __importStar(require("@/actions/builder/header"));
const ACTIONS = __importStar(require("@/actions/builder/main"));
const system_1 = require("@/pages/system");
const index_1 = require("./components/index");
var ModalTypeEnum;
(function (ModalTypeEnum) {
    ModalTypeEnum["condition"] = "condition";
    ModalTypeEnum["function"] = "function";
    ModalTypeEnum["page"] = "page";
    ModalTypeEnum["variable"] = "variable";
})(ModalTypeEnum = exports.ModalTypeEnum || (exports.ModalTypeEnum = {}));
const Modal = (0, styled_components_1.default)(antd_1.Modal) `
  .ant-modal-content {
    height: 100%;
    .ant-modal-body {
      height: calc(100% - 55px - 53px);
      padding: 0;
      overflow: auto;
    }
  }
`;
const mapStateToProps = (store) => ({
    applicationId: store.builder.header.applicationId,
    folderId: store.builder.header.folderId,
    type: store.builder.main.toolbarModalType,
    visible: store.builder.main.toolbarModalVisible,
    conditionPageInfo: store.applications.detail.file.conditions.pageInfo,
    conditions: store.applications.detail.file.conditions.list,
    functionPageInfo: store.applications.detail.file.functions.pageInfo,
    functions: store.applications.detail.file.functions.list,
    variablePageInfo: store.applications.detail.file.variables.pageInfo,
    variables: store.applications.detail.file.variables.list,
    pageNode: store.builder.main.pageNode,
});
const mapDispatchToProps = {
    openModal: ACTIONS.updateToolbarModalVisible,
    templateOpenInPage: ACTIONS.templateOpenInPage,
    updatePageNode: ACTIONS.updatePageNode,
    clearAllCondition: CONDITION_ACTIONS.clearAll,
    deleteCondition: CONDITION_ACTIONS.deleteCondition,
    fetchConditions: CONDITION_ACTIONS.fetchList,
    openConditionEditDrawer: CONDITION_ACTIONS.openEditDrawer,
    updateConditionPageInfo: CONDITION_ACTIONS.updatePaginationInfo,
    clearAllFunction: FUNCTION_ACTIONS.clearAll,
    deleteFunction: FUNCTION_ACTIONS.deleteFunction,
    fetchFunctions: FUNCTION_ACTIONS.fetchList,
    openFunctionEditDrawer: FUNCTION_ACTIONS.openEditDrawer,
    updateFunctionPageInfo: FUNCTION_ACTIONS.updatePaginationInfo,
    openStoreModal: HEADER_ACTIONS.updateStoreModalVisible,
    clearAllVariable: VARIABLE_ACTIONS.clearAll,
    deleteVariable: VARIABLE_ACTIONS.deleteVariable,
    fetchVariables: VARIABLE_ACTIONS.fetchList,
    openVariableEditDrawer: VARIABLE_ACTIONS.openEditDrawer,
    updateVariablePageInfo: VARIABLE_ACTIONS.updatePaginationInfo,
};
const ToolbarModal = (props) => {
    const { applicationId, folderId, type, visible, conditionPageInfo, conditions, functionPageInfo, functions, variablePageInfo, variables, openModal, openStoreModal, clearAllCondition, clearAllFunction, clearAllVariable, deleteCondition, deleteFunction, deleteVariable, fetchConditions, fetchFunctions, fetchVariables, openConditionEditDrawer, openFunctionEditDrawer, openVariableEditDrawer, updateConditionPageInfo, updateFunctionPageInfo, updateVariablePageInfo, pageNode, updatePageNode, templateOpenInPage, } = props;
    const [title, setTitle] = (0, react_1.useState)('');
    const [button, setButton] = (0, react_1.useState)('');
    const [dataSource, setDataSource] = (0, react_1.useState)();
    const [isPage, setIsPage] = (0, react_1.useState)(false);
    const [newPageNode, setPageNode] = (0, react_1.useState)(pageNode);
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { builder, condition, function: func, variable } = locale.business;
    (0, react_1.useEffect)(() => {
        setPageNode(pageNode);
    }, [pageNode]);
    (0, react_1.useEffect)(() => {
        if (type) {
            setIsPage(type === ModalTypeEnum.page);
        }
    }, [type]);
    (0, react_1.useEffect)(() => {
        const pageInfo = {
            page: 1,
            size: 5,
            total: 0,
        };
        if (type === ModalTypeEnum.condition)
            updateConditionPageInfo(pageInfo);
        if (type === ModalTypeEnum.function)
            updateFunctionPageInfo(pageInfo);
        if (type === ModalTypeEnum.variable)
            updateVariablePageInfo(pageInfo);
    }, [type]);
    (0, react_1.useEffect)(() => {
        if (type === ModalTypeEnum.condition) {
            setTitle(condition.name);
            setButton(condition.add);
            setDataSource(conditions || []);
        }
        if (type === ModalTypeEnum.function) {
            setTitle(func.name);
            setButton(func.add);
            setDataSource(functions || []);
        }
        if (type === ModalTypeEnum.variable) {
            setTitle(variable.name);
            setButton(variable.add);
            setDataSource(variables || []);
        }
        if (type === ModalTypeEnum.page) {
            setTitle(builder.pageSetting);
        }
    }, [type, conditions, functions, variables]);
    const open = (0, react_1.useMemo)(() => visible && type !== 'variableBind', [visible, type]);
    const paginationInfo = (0, react_1.useMemo)(() => ({
        condition: conditionPageInfo,
        function: functionPageInfo,
        variable: variablePageInfo,
    }), [conditionPageInfo, functionPageInfo, variablePageInfo]);
    const handleFetch = (0, react_1.useCallback)((params) => {
        if (type === ModalTypeEnum.condition)
            fetchConditions(params);
        if (type === ModalTypeEnum.function)
            fetchFunctions(params);
        if (type === ModalTypeEnum.variable)
            fetchVariables(params);
    }, [type]);
    const handleEdit = (editType, entity) => {
        if (type === ModalTypeEnum.condition) {
            openConditionEditDrawer(true, entity, editType);
        }
        if (type === ModalTypeEnum.function) {
            openFunctionEditDrawer(true, entity, editType);
        }
        if (type === ModalTypeEnum.variable) {
            openVariableEditDrawer(true, entity, editType);
        }
    };
    const handleDelete = (id, params) => {
        if (applicationId && id) {
            const deleteParams = {
                applicationId,
                id,
                status: true,
            };
            if (type === ModalTypeEnum.condition)
                deleteCondition(deleteParams, () => handleFetch(params));
            if (type === ModalTypeEnum.function)
                deleteFunction(deleteParams, () => handleFetch(params));
            if (type === ModalTypeEnum.variable)
                deleteVariable(deleteParams, () => handleFetch(params));
        }
    };
    const handleCancel = () => {
        openModal(false, '');
        clearAllCondition();
        clearAllFunction();
        clearAllVariable();
    };
    const handleOk = () => {
        // TODO:　need to clean
        if (isPage) {
            updatePageNode(newPageNode);
        }
        handleCancel();
    };
    const handleOpenTemplateModal = (value, target) => {
        openStoreModal(value, target);
        templateOpenInPage(true);
    };
    return (react_1.default.createElement(Modal, { destroyOnClose: true, maskClosable: false, width: isPage ? '400px' : '60%', zIndex: 100, title: title, visible: open, onOk: handleOk, onCancel: handleCancel, style: { height: 600 } }, isPage ? (react_1.default.createElement(index_1.Page, { pageNode: newPageNode, openModal: handleOpenTemplateModal, onPageNodeChange: (data) => setPageNode(data) })) : (react_1.default.createElement(index_1.List, { type: type, applicationId: applicationId, folderId: folderId, button: button, paginationInfo: paginationInfo, dataSource: dataSource, onDelete: handleDelete, onEdit: handleEdit, onFetch: handleFetch }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(ToolbarModal);
