"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 list_1 = require("@/actions/applications/detail/packages/list");
const ACTIONS = __importStar(require("@/actions/applications/detail/settings/builder/component"));
const system_1 = require("@/pages/system");
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 mapStateToProps = (store) => ({
    components: store.applications.detail.packages.list.componentList,
    pageInfo: store.applications.detail.packages.list.pageInfo,
    visible: store.applications.detail.settings.builder.components.modalVisible,
});
const mapDispatchToProps = {
    clear: ACTIONS.clearAll,
    openEditor: ACTIONS.updateEditorVisible,
    openModal: ACTIONS.updateModalVisible,
    clearAll: list_1.clearAll,
    fetchComponents: list_1.fetchComponentsAction,
};
const Main = (props) => {
    const { components, pageInfo, visible, clear, clearAll, fetchComponents, openEditor, openModal } = props;
    const [search, setSearch] = (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)(() => {
        return () => {
            clear();
            // packages related
            clearAll();
        };
    }, []);
    (0, react_1.useEffect)(() => {
        if (applicationId) {
            fetchComponents({
                applicationId,
                page: pageInfo.page,
                size: pageInfo.size,
                search,
            });
        }
    }, [applicationId, pageInfo.page, search]);
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
        },
        {
            title: global.liveVersion,
            dataIndex: 'release',
            key: 'release',
            render: (release, record) => {
                return (react_1.default.createElement(react_1.default.Fragment, null,
                    release ? react_1.default.createElement(antd_1.Tag, { color: "orange" }, release) : '- ',
                    record.base && (react_1.default.createElement(antd_1.Tooltip, { title: global.newVersion },
                        react_1.default.createElement(icons_1.ArrowUpOutlined, { style: { color: '#8adf5e' } })))));
            },
        },
        {
            title: global.actions,
            dataIndex: '',
            width: 80,
            render: (record) => (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: category.addToComponentBar, onClick: () => openEditor(true, { id: record.id, name: record.name }) },
                    react_1.default.createElement(icons_1.BlockOutlined, null)))),
        },
    ];
    return (react_1.default.createElement(antd_1.Modal, { bodyStyle: {
            height: 765,
        }, centered: true, destroyOnClose: true, footer: null, maskClosable: false, title: category.addComponent, visible: visible, width: "60%", zIndex: 99, onCancel: () => openModal(false) },
        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, { rowKey: "id", columns: columns, dataSource: components, pagination: pageInfo.total > pageInfo.size
                ? {
                    position: ['bottomCenter'],
                    current: pageInfo.page,
                    pageSize: pageInfo.size,
                    total: pageInfo.total,
                }
                : false, onChange: (pagination) => {
                fetchComponents({
                    applicationId,
                    page: pagination.current || 1,
                    size: pagination.pageSize || 10,
                    search,
                });
            } })));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Main);
