"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 antd_1 = require("antd");
const ACTIONS = __importStar(require("@/actions/applications/detail/packages/detail"));
const store_1 = require("@/constants/store");
const system_1 = require("@/pages/system");
const index_1 = require("@/utils/index");
const { TabPane } = antd_1.Tabs;
const mapStateToProps = (store) => ({
    applicationId: store.applications.detail.settings.app.applicationId,
    componentInfo: store.applications.detail.packages.detail.componentInfo,
    loading: store.applications.detail.packages.detail.versionList.loading,
    versions: store.applications.detail.packages.detail.versionList.versions,
    pageInfo: store.applications.detail.packages.detail.versionList.pageInfo,
    fileDetail: store.applications.detail.packages.detail.fileDetail,
});
const mapDispatchToProps = {
    updateVersionListState: ACTIONS.updateVersionListState,
    fetchVersionList: ACTIONS.fetchComponentVersionsAction,
    updateVersionStatus: ACTIONS.updateComponentVersionStatusAction,
    liveVersion: ACTIONS.liveComponentVersionAction,
    editVersion: ACTIONS.updateVersionDrawerState,
    viewVersion: ACTIONS.updateVersionDrawerState,
};
const VersionList = (props) => {
    const { applicationId, componentInfo, loading, versions, pageInfo, fileDetail, updateVersionListState, fetchVersionList, updateVersionStatus, liveVersion, editVersion, viewVersion, } = props;
    // url params
    const { fileId } = (0, index_1.getLocationIfo)((0, react_router_dom_1.useLocation)());
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, package: packageI18n, version } = locale.business;
    (0, react_1.useEffect)(() => {
        if (applicationId && fileId) {
            updateVersionListState({
                applicationId,
                fileId,
            });
            fetchVersionList({});
        }
    }, [applicationId, fileId]);
    const onClickPublish = (0, react_1.useCallback)((id) => {
        if (applicationId)
            updateVersionStatus({
                applicationId,
                id,
                status: 'release',
            });
    }, [applicationId, updateVersionStatus]);
    const onClickLive = (0, react_1.useCallback)((id, versionNumber) => {
        if (applicationId)
            liveVersion({
                applicationId,
                id,
                versionNumber,
            });
    }, [applicationId, liveVersion]);
    const onClickViewOrEdit = (0, react_1.useCallback)((type, id) => {
        if (type === 'edit') {
            editVersion({
                open: true,
                type: 'edit',
                data: {
                    versionId: id,
                },
            });
        }
        else {
            viewVersion({
                open: true,
                type: 'view',
                data: {
                    versionId: id,
                },
            });
        }
    }, [viewVersion, editVersion]);
    const columns = [
        {
            title: version.name,
            dataIndex: 'version',
            key: 'version',
            render: (version, record) => {
                const { isLiveVersion } = record;
                return (react_1.default.createElement(antd_1.Space, { wrap: false, size: 6 },
                    version,
                    isLiveVersion && (react_1.default.createElement(antd_1.Tag, { color: "cyan", style: { marginRight: 0 } }, "Live"))));
            },
        },
        {
            title: global.creator,
            dataIndex: 'creator',
            key: 'creator',
            render: (creator) => {
                return creator ? creator.account : '--';
            },
        },
        {
            title: global.updateTime,
            dataIndex: 'updateTime',
            key: 'updateTime',
            width: 200,
            render: (text) => (0, index_1.periodFormat)(text, 'unknown'),
        },
        {
            title: version.status,
            dataIndex: 'status',
            key: 'status',
            width: 100,
        },
        {
            title: global.actions,
            key: 'action',
            align: 'center',
            width: 200,
            render: (record) => {
                var _a;
                const isRefer = (_a = fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.tags) === null || _a === void 0 ? void 0 : _a.find((item) => item.type === store_1.StoreGoodsPurchaseType.reference);
                const actionList = [];
                if (isRefer) {
                    actionList.push(react_1.default.createElement(antd_1.Button, { type: "link", title: "View", size: "small", onClick: () => onClickViewOrEdit('view', record.id) }, global.view));
                }
                else {
                    if (record.status === 'base') {
                        actionList.push(react_1.default.createElement(antd_1.Button, { type: "link", title: "Edit", size: "small", onClick: () => onClickViewOrEdit('edit', record.id) }, global.edit));
                    }
                    else {
                        actionList.push(react_1.default.createElement(antd_1.Button, { type: "link", title: "View", size: "small", onClick: () => onClickViewOrEdit('view', record.id) }, global.edit));
                    }
                    if (record.status === 'release') {
                        if (!record.isLiveVersion) {
                            actionList.push(react_1.default.createElement(antd_1.Popconfirm, { title: packageI18n.setVersionLiveTip, arrowPointAtCenter: true, onConfirm: () => {
                                    onClickLive(componentInfo.id, record.versionNumber);
                                } },
                                react_1.default.createElement(antd_1.Button, { type: "link", title: "Live", size: "small" }, version.live)));
                        }
                    }
                    else {
                        actionList.push(react_1.default.createElement(antd_1.Button, { type: "link", size: "small", title: "Publish", onClick: () => onClickPublish(record.id) }, record.status === 'discard' ? version.republish : version.publish));
                    }
                }
                return (react_1.default.createElement(antd_1.Space, { size: [2, 2], align: "center", wrap: true }, actionList.map((el, index) => (react_1.default.createElement(react_1.default.Fragment, { key: index }, el)))));
            },
        },
    ];
    return (react_1.default.createElement(antd_1.Tabs, { defaultActiveKey: "versions", style: { marginTop: 24 } },
        react_1.default.createElement(TabPane, { tab: global.versions, key: "versions" },
            react_1.default.createElement(antd_1.Table, { dataSource: versions, columns: columns, loading: loading, rowKey: "id", pagination: pageInfo.total > pageInfo.size
                    ? { current: pageInfo.page, pageSize: pageInfo.size, total: pageInfo.total }
                    : false }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(VersionList);
