"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 icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const ACTIONS = __importStar(require("@/actions/builder/header"));
const PAGE_ACTIONS = __importStar(require("@/actions/builder/main"));
const index_1 = require("@/components/index");
const system_1 = require("@/pages/system");
const Modal = (0, styled_components_1.default)(antd_1.Modal) `
  .ant-modal-content {
    height: 100%;
    .ant-modal-body {
      height: calc(100% - 110px);
      padding: 12px;
      overflow: auto;
    }
  }
`;
const mapStateToProps = (store) => ({
    applicationId: store.builder.header.applicationId,
    folderId: store.builder.header.folderId,
    contentId: store.builder.header.contentId,
    visible: store.builder.header.mockModalVisible,
    loading: store.builder.header.mockLoading,
    editStatus: store.builder.main.editStatus,
    file: store.builder.main.file,
    mock: store.builder.main.pageContent.mock,
});
const mapDispatchToProps = {
    updateMockModalVisible: ACTIONS.updateMockModalVisible,
    saveMock: ACTIONS.saveMock,
    publishMock: ACTIONS.publishMock,
    fetchContent: PAGE_ACTIONS.fetchContent,
    updatePageEditStatus: PAGE_ACTIONS.updateEditState,
};
const Mock = (props) => {
    const { applicationId, folderId, contentId, visible, loading, editStatus, file, mock, updatePageEditStatus, updateMockModalVisible, saveMock, fetchContent, publishMock, } = props;
    const [mockId, setMockId] = (0, react_1.useState)();
    const [mockMode, setMockMode] = (0, react_1.useState)(false);
    const [jsonData, setJsonData] = (0, react_1.useState)({});
    (0, react_1.useEffect)(() => {
        if (visible) {
            if (mock && (mock === null || mock === void 0 ? void 0 : mock.schemas) && mock.schemas.length > 0) {
                setJsonData(mock);
            }
            setMockId(mock === null || mock === void 0 ? void 0 : mock.id);
            setMockMode(mock === null || mock === void 0 ? void 0 : mock.enable);
        }
        else {
            // clear all when hidden
            setMockMode(mock === null || mock === void 0 ? void 0 : mock.enable);
            setJsonData({});
        }
    }, [visible, mock]);
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, mock: language } = locale.business;
    const handleClose = (0, react_1.useCallback)(() => {
        updateMockModalVisible(false);
    }, []);
    const handleChange = (0, react_1.useCallback)((status) => {
        setMockMode(status);
        // update page edit status
        if (!editStatus)
            updatePageEditStatus(true);
    }, [editStatus]);
    const handlePublish = (0, react_1.useCallback)(() => {
        if (applicationId && mockId) {
            saveMock({
                applicationId,
                folderId,
                contentId: contentId || '',
                name: `mock_${contentId}`,
                content: Object.assign(Object.assign({}, jsonData), { enable: mockMode }),
            }, (id) => {
                if (id)
                    publishMock({
                        applicationId,
                        id,
                        status: 'release',
                    });
            });
        }
    }, [applicationId, folderId, contentId, mockId, jsonData, mockMode, saveMock, publishMock]);
    const handleConfirm = () => {
        if (applicationId && folderId) {
            saveMock({
                applicationId,
                folderId,
                contentId: contentId || '',
                name: `mock_${contentId}`,
                content: Object.assign(Object.assign({}, jsonData), { enable: mockMode }),
            }, () => {
                handleClose();
                setTimeout(() => {
                    fetchContent({
                        applicationId,
                        id: contentId,
                        type: file.type,
                    });
                }, 50);
            });
        }
    };
    const footer = (0, react_1.useMemo)(() => (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(antd_1.Checkbox, { checked: mockMode, onChange: (e) => handleChange(e.target.checked), style: { marginRight: 8 } },
            language.enableMockMode,
            ' ',
            react_1.default.createElement(antd_1.Tooltip, { title: language.enableMockTips },
                react_1.default.createElement(icons_1.QuestionCircleOutlined, null))),
        react_1.default.createElement(antd_1.Button, { type: "ghost", onClick: handleClose }, global.cancel),
        react_1.default.createElement(antd_1.Button, { type: "primary", onClick: handleConfirm }, global.confirm),
        react_1.default.createElement(antd_1.Button, { type: "primary", disabled: !mockId, onClick: handlePublish }, global.publish))), [mockMode, mockId]);
    return (react_1.default.createElement(Modal, { title: language.title, destroyOnClose: true, width: 1048, visible: visible, footer: footer, onCancel: handleClose, style: { height: '70%' } }, loading ? (react_1.default.createElement(antd_1.Spin, { spinning: loading })) : (react_1.default.createElement(index_1.JSONEditor, { jsonData: jsonData, refreshFlag: jsonData, readOnly: true, options: { mode: 'code' } }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Mock);
