"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_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 system_1 = require("@/pages/system");
const empty_check_1 = require("@/utils/empty-check");
const { Search } = antd_1.Input;
const Header = styled_components_1.default.div `
  display: flex;
  justify-content: flex-end;
  margin-bottom: 12px;
`;
const Table = (0, styled_components_1.default)(antd_1.Table) `
  .ant-table-pagination.ant-pagination {
    margin: 16px 0 0;
  }
`;
const Main = (props) => {
    const { list, loading, pageInfo, type, visible, onClose, onFetch, onSave } = props;
    const [search, setSearch] = (0, react_1.useState)('');
    const [selectedRowKeys, setSelectedRowKeys] = (0, react_1.useState)([]);
    const { applicationId } = (0, react_router_dom_1.useParams)();
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { category, global, package: packageI18n } = locale.business;
    (0, react_1.useEffect)(() => {
        if (visible && applicationId && typeof onFetch === 'function') {
            onFetch({
                applicationId,
                page: pageInfo.page,
                size: pageInfo.size,
                search,
            });
        }
        else {
            setSearch('');
            setSelectedRowKeys([]);
        }
    }, [applicationId, pageInfo.page, search, visible]);
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
        },
    ];
    if (type) {
        const newColumn = type !== 'component'
            ? {
                title: global.idLabel,
                dataIndex: 'id',
                width: 200,
            }
            : {
                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' } })))));
                },
            };
        columns.push(newColumn);
    }
    const handleCancel = () => {
        if (onClose && typeof onClose === 'function')
            onClose();
    };
    const handleConfirm = () => {
        if (!(0, empty_check_1.objectEmptyCheck)(selectedRowKeys)) {
            const setting = selectedRowKeys.map((id) => {
                var _a;
                return ({
                    category: {},
                    id,
                    name: ((_a = list.find((item) => item.id === id)) === null || _a === void 0 ? void 0 : _a.name) || '',
                    status: false,
                });
            });
            onSave({
                applicationId,
                type,
                setting,
            });
        }
        setTimeout(() => handleCancel(), 250);
    };
    const onSelectChange = (newSelectedRowKeys) => {
        setSelectedRowKeys(newSelectedRowKeys);
    };
    const title = (0, react_1.useMemo)(() => {
        const typeTitleMap = {
            component: category.addComponent,
            template: category.addTemplate,
            page: category.addPage,
        };
        return typeTitleMap[type];
    }, [type, category]);
    return (react_1.default.createElement(antd_1.Modal, { bodyStyle: {
            height: 740,
        }, centered: true, destroyOnClose: true, maskClosable: false, title: title, visible: visible, width: "60%", onCancel: handleCancel, onOk: handleConfirm },
        react_1.default.createElement(Header, null,
            react_1.default.createElement(Search, { allowClear: true, placeholder: packageI18n.inputNameTips, onSearch: setSearch, style: { width: 250 } })),
        react_1.default.createElement(Table, { columns: columns, dataSource: list, loading: loading, rowKey: "id", rowSelection: {
                selectedRowKeys,
                onChange: onSelectChange,
            }, pagination: pageInfo.total > pageInfo.size
                ? {
                    position: ['bottomCenter'],
                    current: pageInfo.page,
                    pageSize: pageInfo.size,
                    total: pageInfo.total,
                    showSizeChanger: false,
                }
                : false, onChange: (pagination) => {
                onFetch({
                    applicationId,
                    page: pagination.current || 1,
                    size: pagination.pageSize || 10,
                    search,
                });
            } })));
};
exports.default = Main;
