"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_redux_1 = require("react-redux");
const icons_1 = require("@ant-design/icons");
const ACTIONS = __importStar(require("@/actions/builder/header"));
const Main_1 = require("@/pages/builder/header/Main");
const system_1 = require("@/pages/system");
const PAGE_ACTIONS = __importStar(require("@/store/actions/builder/main"));
const components_1 = require("./components");
const mapStateToProps = (store) => ({
    applicationId: store.builder.header.applicationId,
    folderId: store.builder.header.folderId,
    contentId: store.builder.header.contentId,
    mock: store.builder.main.mock,
    editStatus: store.builder.main.editStatus,
    saveLoading: store.builder.main.saveLoading,
    publishLoading: store.builder.main.publishLoading,
});
const mapDispatchToProps = {
    saveMock: ACTIONS.saveMock,
    savePageContent: PAGE_ACTIONS.saveContent,
    publishPageContent: PAGE_ACTIONS.publishContent,
};
const Catalog = (props) => {
    const { applicationId, folderId, contentId, mock, editStatus, saveLoading, publishLoading, saveMock, savePageContent, publishPageContent, } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { mock: mockI18n } = locale.business;
    const generateMockSaveParams = (0, react_1.useCallback)(() => {
        return {
            applicationId,
            folderId: folderId || '',
            contentId: contentId || '',
            name: `mock_${contentId}`,
            content: mock,
        };
    }, [applicationId, folderId, contentId, mock]);
    const handleSave = () => {
        const mockDataValid = mock.schemas && mock.schemas.length > 0;
        if (mockDataValid) {
            const params = generateMockSaveParams();
            saveMock(params, () => {
                savePageContent();
            });
        }
        else {
            savePageContent();
        }
    };
    const handlePublish = () => {
        const mockDataValid = mock && mock.schemas && mock.schemas.length > 0;
        if (mockDataValid) {
            const params = generateMockSaveParams();
            saveMock(params, () => {
                publishPageContent();
            });
        }
        else {
            publishPageContent();
        }
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        (mock === null || mock === void 0 ? void 0 : mock.enable) && (react_1.default.createElement(Main_1.StyledIcon, { style: { backgroundColor: '#ffffff', color: '#ff5918', cursor: 'default' } },
            react_1.default.createElement(icons_1.BugOutlined, null),
            react_1.default.createElement(Main_1.IconMsg, null, mockI18n.mockMode))),
        react_1.default.createElement(components_1.DSL, null),
        react_1.default.createElement(components_1.Mock, null),
        react_1.default.createElement(components_1.More, null),
        react_1.default.createElement(components_1.Preview, null),
        react_1.default.createElement(components_1.Save, { loading: saveLoading, editStatus: editStatus, onSave: handleSave }),
        react_1.default.createElement(components_1.Publish, { loading: publishLoading, onPublish: handlePublish })));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Catalog);
