"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 react_router_dom_1 = require("react-router-dom");
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/applications/detail/packages/list"));
const RESOURCES_ACTIONS = __importStar(require("@/actions/applications/detail/resources/groups"));
const index_1 = require("@/components/index");
const index_2 = require("@/constants/index");
const system_1 = require("@/pages/system");
const index_3 = require("@/utils/index");
const Table = (0, styled_components_1.default)(antd_1.Table) `
  .ant-table-pagination.ant-pagination {
    margin: 16px 0 -8px;
  }
`;
const getGroup = (tags, groups) => {
    return groups.find((item) => tags.findIndex((tag) => tag.type === 'resourceGroup' && item.id === tag.resourceGroupId) > -1);
};
const mapStateToProps = (store) => ({
    applicationId: store.applications.detail.settings.app.applicationId,
    groups: store.applications.detail.resources.groups.groupList,
    loading: store.applications.detail.packages.list.loading,
    pageInfo: store.applications.detail.packages.list.pageInfo,
    componentList: store.applications.detail.packages.list.componentList,
    selectPackage: store.applications.detail.packages.list.selectPackage,
});
const mapDispatchToProps = {
    deleteComponent: ACTIONS.deleteComponentAction,
    fetchComponentList: ACTIONS.fetchComponentsAction,
    openDrawer: ACTIONS.updateComponentDrawerState,
    fetchGroups: RESOURCES_ACTIONS.fetchResourcesGroups,
};
const PackageList = (props) => {
    const { applicationId, groups, loading, pageInfo = { page: 1, size: 10, total: 1 }, componentList, selectPackage, deleteComponent, fetchComponentList, fetchGroups, openDrawer, openQuicklyModal, } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, package: packageI18n, resource } = locale.business;
    (0, react_1.useEffect)(() => {
        if (applicationId) {
            fetchGroups({
                applicationId,
            });
        }
    }, [applicationId]);
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
            key: 'name',
            render: (text, record) => {
                var _a;
                return (react_1.default.createElement(react_1.default.Fragment, null,
                    ((_a = record.tags) === null || _a === void 0 ? void 0 : _a.find((item) => item.type === index_2.StoreGoodsPurchaseType.reference)) && (react_1.default.createElement(antd_1.Tag, { color: index_2.suffixTagColor.refer, style: { zoom: 0.8 } }, "refer")),
                    record.online && (react_1.default.createElement(antd_1.Tag, { color: "cyan", style: { zoom: 0.8 } }, "inStore")),
                    react_1.default.createElement(react_router_dom_1.Link, { to: `/applications/${applicationId}/packages/detail?fileId=${record.id}&name=${encodeURIComponent(record.name)}`, style: { paddingRight: 8 } }, text)));
            },
        },
        {
            title: global.liveVersion,
            dataIndex: 'release',
            key: 'release',
            width: 120,
            render: (release, record) => {
                return (react_1.default.createElement(react_1.default.Fragment, null,
                    release ? react_1.default.createElement(antd_1.Tag, { color: "orange" }, release) : react_1.default.createElement(antd_1.Tag, null, "0.0.0"),
                    record.base && (react_1.default.createElement(antd_1.Tooltip, { title: global.newVersion },
                        react_1.default.createElement(icons_1.ArrowUpOutlined, { style: { color: '#8adf5e' } })))));
            },
        },
        {
            title: resource.group,
            dataIndex: 'tags',
            key: 'tags',
            render: (tags) => {
                const group = getGroup(tags, groups);
                return group ? group.name : '-';
            },
        },
        {
            title: global.creator,
            dataIndex: 'creator',
            key: 'creator',
            render: (creator) => {
                return (creator === null || creator === void 0 ? void 0 : creator.account) || '-';
            },
        },
        {
            title: global.updateTime,
            dataIndex: 'updateTime',
            key: 'updateTime',
            width: 180,
            render: (text) => (0, index_3.periodFormat)(text, 'unknown'),
        },
        {
            title: global.actions,
            key: 'actions',
            width: 90,
            render: (record) => {
                const { id, release } = record;
                return (react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement(antd_1.Popconfirm, { title: `${global.deleteMsg}${record.name}?`, onConfirm: () => {
                            if (applicationId)
                                deleteComponent({
                                    applicationId,
                                    id,
                                });
                        }, disabled: !!release, okText: global.yes, cancelText: global.no, placement: "topRight" },
                        react_1.default.createElement(index_1.DeleteButton, { type: "default", size: "small", shape: "circle", disabled: !!release, title: global.remove, style: { marginLeft: 8 } }))));
            },
        },
    ];
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { style: { marginBottom: 12, display: 'flex', justifyContent: 'flex-end' } },
            selectPackage === 'component' && (react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => (openQuicklyModal ? openQuicklyModal(true) : null), style: { marginRight: 8 } },
                react_1.default.createElement(icons_1.ThunderboltOutlined, null),
                " ",
                packageI18n.quickly)),
            react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => openDrawer({ open: true, type: 'add' }) },
                react_1.default.createElement(icons_1.PlusOutlined, null),
                " ",
                packageI18n[selectPackage] || packageI18n.component)),
        react_1.default.createElement(Table, { dataSource: componentList, columns: columns, loading: loading, rowKey: "id", pagination: pageInfo.total > pageInfo.size
                ? {
                    position: ['bottomCenter'],
                    current: pageInfo.page,
                    pageSize: pageInfo.size,
                    total: pageInfo.total,
                }
                : false, onChange: (pagination) => {
                fetchComponentList({
                    applicationId,
                    page: pagination.current || 1,
                    size: pagination.pageSize || 10,
                });
            } })));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(PackageList);
