"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 antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const ACTIONS = __importStar(require("@/actions/applications/detail/file/conditions"));
const index_1 = require("@/components/index");
const index_2 = require("@/constants/index");
const system_1 = require("@/pages/system");
const Toolbar = styled_components_1.default.div `
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
`;
const mapStateToProps = (state) => ({
    applicationId: state.builder.header.applicationId,
    folderId: state.builder.header.folderId,
    pageInfo: state.applications.detail.file.conditions.pageInfo,
    list: state.applications.detail.file.conditions.list,
    loading: state.applications.detail.file.conditions.loading,
});
const mapDispatchToProps = {
    fetchList: ACTIONS.fetchList,
};
const Main = (props) => {
    const { loading, visible, conditions, applicationId, folderId, list, pageInfo, fetchList, onClose, onChange, } = props;
    const [group, setGroup] = (0, react_1.useState)(index_2.ScopeEnum.project);
    const [selectedRows, setSelectedRows] = (0, react_1.useState)([]);
    // i18n
    const { locale: i18n } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, condition } = i18n.business;
    (0, react_1.useEffect)(() => {
        if (visible && applicationId) {
            const params = {
                applicationId,
                page: pageInfo.page,
                size: pageInfo.size,
            };
            if (group === index_2.ScopeEnum.project) {
                params.folderId = folderId;
            }
            fetchList(params);
        }
    }, [visible, applicationId, folderId, group]);
    (0, react_1.useEffect)(() => {
        setSelectedRows(conditions);
    }, [conditions]);
    const handleGroupChange = (0, react_1.useCallback)((group) => {
        setGroup(group);
    }, []);
    const handleClose = () => {
        onClose();
    };
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: global.type,
            dataIndex: 'type',
            key: 'type',
            width: 100,
            render: (_text, record) => {
                var _a;
                const type = ((_a = record === null || record === void 0 ? void 0 : record.content.schemas[0]) === null || _a === void 0 ? void 0 : _a.type) || '';
                return react_1.default.createElement("span", null, type ? index_2.ConditionTypeEnum[type] : '');
            },
        },
    ];
    return (react_1.default.createElement(index_1.OperationDrawer, { open: visible, onClose: handleClose, title: condition.selectCondition, width: 480, actions: react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => {
                onChange(selectedRows);
                handleClose();
            } }, global.apply) },
        react_1.default.createElement(index_1.Group, null,
            react_1.default.createElement(Toolbar, null,
                react_1.default.createElement(index_1.ScopeSelector, { scope: group, onScopeChange: handleGroupChange })),
            react_1.default.createElement(antd_1.Table, { size: "small", bordered: false, loading: loading, columns: columns, rowKey: (record) => record.contentId, dataSource: list, rowSelection: {
                    type: 'checkbox',
                    selectedRowKeys: selectedRows.map((condition) => condition.content.id) || [],
                    onChange: (_selectedRowKeys, selectedRows) => {
                        setSelectedRows(selectedRows);
                    },
                }, pagination: pageInfo.total > pageInfo.size
                    ? {
                        position: ['bottomCenter'],
                        current: pageInfo.page,
                        pageSize: pageInfo.size,
                        total: pageInfo.total,
                    }
                    : false, onChange: (pagination) => {
                    fetchList({
                        applicationId,
                        page: pagination.current || 1,
                        size: pagination.pageSize || 10,
                    });
                } }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Main);
