"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 react_router_dom_1 = require("react-router-dom");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const ACTIONS = __importStar(require("@/actions/applications/detail/packages/detail"));
const PROJECT_ACTIONS = __importStar(require("@/actions/projects/content"));
const application_1 = require("@/apis/application");
const index_1 = require("@/components/index");
const index_2 = require("@/constants/index");
const system_1 = require("@/pages/system");
const location_info_1 = require("@/utils/location-info");
const index_3 = require("./components/index");
const mapStateToProps = (store) => ({
    applicationId: store.applications.detail.settings.app.applicationId,
    componentInfo: store.applications.detail.packages.detail.componentInfo,
    fileDetail: store.applications.detail.packages.detail.fileDetail,
});
const mapDispatchToProps = {
    clearAll: ACTIONS.clearAll,
    addVersion: ACTIONS.updateVersionDrawerState,
    openCloudSyncDrawer: ACTIONS.updateCloudSyncDrawerOpenStatus,
    updateComponentOnlineStatus: ACTIONS.updateComponentOnlineStatus,
    fetchFileDetail: ACTIONS.fetchFileDetail,
    commitFileToStore: PROJECT_ACTIONS.commitFileToStore,
    offlineFileFromStore: PROJECT_ACTIONS.offlineFileFromStore,
};
const ComponentDetail = (props) => {
    var _a;
    const { applicationId, componentInfo, fileDetail, clearAll, addVersion, openCloudSyncDrawer, updateComponentOnlineStatus, fetchFileDetail, commitFileToStore, offlineFileFromStore, } = props;
    const { id = '', type, online } = componentInfo;
    // url params
    const { fileId, name: componentName } = (0, location_info_1.getLocationIfo)((0, react_router_dom_1.useLocation)());
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, application, store, version } = locale.business;
    const isRefer = (_a = fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.tags) === null || _a === void 0 ? void 0 : _a.find((item) => item.type === index_2.StoreGoodsPurchaseType.reference);
    (0, react_1.useEffect)(() => {
        (0, application_1.clearResourcesCache)();
        return () => {
            clearAll();
        };
    }, []);
    (0, react_1.useEffect)(() => {
        if (applicationId && fileId)
            fetchFileDetail({ applicationId, ids: [fileId] });
    }, [applicationId]);
    const handleCommit = () => {
        antd_1.Modal.confirm({
            title: store.commitTitle,
            content: store.commitMsg,
            onOk: () => {
                if (applicationId && fileId)
                    commitFileToStore({
                        id: fileId,
                        applicationId,
                        type: index_2.FileType.package,
                    }, () => {
                        updateComponentOnlineStatus(true);
                    });
            },
            okText: global.yes,
            cancelText: global.no,
        });
    };
    const handleRevoke = () => {
        antd_1.Modal.confirm({
            title: store.revokeTitle,
            content: store.revokeMsg,
            onOk: () => {
                if (applicationId && fileId)
                    offlineFileFromStore({
                        id: fileId,
                        applicationId,
                    }, () => {
                        updateComponentOnlineStatus(false);
                    });
            },
            okText: global.yes,
            cancelText: global.no,
        });
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(index_1.FoxPageContent, { breadcrumb: react_1.default.createElement(index_1.FoxPageBreadcrumb, { breadCrumb: [
                    { name: application.applicationList, link: '/#/workspace/applications' },
                    {
                        name: global.packages,
                        link: `/#/applications/${applicationId}/packages/list`,
                    },
                    { name: componentName },
                ] }) },
            react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("div", { style: { marginBottom: 12, display: 'flex', justifyContent: 'flex-end' } }, !isRefer && (react_1.default.createElement(react_1.default.Fragment, null,
                    type === 'component' && (react_1.default.createElement(react_1.default.Fragment, null, online ? (react_1.default.createElement(antd_1.Button, { type: "default", style: { marginRight: 8 }, onClick: handleRevoke },
                        react_1.default.createElement(icons_1.ArrowDownOutlined, null),
                        store.revoke)) : (react_1.default.createElement(antd_1.Button, { type: "default", style: { marginRight: 8 }, onClick: handleCommit },
                        react_1.default.createElement(icons_1.ArrowUpOutlined, null),
                        store.commit)))),
                    (fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.type) === 'component' && (react_1.default.createElement(antd_1.Button, { type: "default", style: { marginRight: 8 }, onClick: () => openCloudSyncDrawer(true) },
                        react_1.default.createElement(icons_1.CloudSyncOutlined, null),
                        " ",
                        global.update)),
                    react_1.default.createElement(antd_1.Button, { type: "primary", disabled: !id, onClick: () => addVersion({ open: true, type: 'add' }) },
                        react_1.default.createElement(icons_1.PlusOutlined, null),
                        " ",
                        version.add)))),
                react_1.default.createElement(index_3.Header, null),
                react_1.default.createElement(index_3.List, null))),
        react_1.default.createElement(index_3.VersionEditDrawer, null),
        react_1.default.createElement(index_3.CloudSyncDrawer, null)));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(ComponentDetail);
