"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/functions"));
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) => ({
    list: state.applications.detail.file.functions.list,
    loading: state.applications.detail.file.functions.loading,
    pageInfo: state.applications.detail.file.functions.pageInfo,
});
const mapDispatchToProps = {
    clearAll: ACTIONS.clearAll,
    fetchList: ACTIONS.fetchList,
};
const ConditionSelect = (props) => {
    const { visible, funcId, applicationId, folderId, list, loading, pageInfo, fetchList, onChange, onClose, clearAll, } = props;
    const [group, setGroup] = (0, react_1.useState)(!!folderId ? index_2.ScopeEnum.project : index_2.ScopeEnum.application);
    const [selectedRow, setSelectedRow] = (0, react_1.useState)(undefined);
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, variable } = locale.business;
    (0, react_1.useEffect)(() => {
        if (applicationId && visible) {
            const params = {
                applicationId,
                page: pageInfo.page,
                size: pageInfo.size,
            };
            if (!!folderId && group === index_2.ScopeEnum.application) {
                params.type = 'live';
            }
            if (group === index_2.ScopeEnum.project) {
                params.folderId = folderId;
            }
            fetchList(params);
        }
        else {
            clearAll();
        }
    }, [applicationId, folderId, group, visible]);
    (0, react_1.useEffect)(() => {
        if (funcId && visible) {
            setSelectedRow(list.find((item) => item.contentId === funcId));
        }
    }, [funcId, visible, list]);
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: global.type,
            dataIndex: 'type',
            key: 'type',
            render: (_text, record) => {
                var _a, _b, _c;
                const type = ((_c = (_b = (_a = record.content) === null || _a === void 0 ? void 0 : _a.schemas[0]) === null || _b === void 0 ? void 0 : _b.props) === null || _c === void 0 ? void 0 : _c.async) || '';
                return react_1.default.createElement("span", null, type ? 'async' : 'sync');
            },
        },
    ];
    return (react_1.default.createElement(index_1.OperationDrawer, { open: visible, onClose: onClose, title: variable.selectFunction, width: 480, destroyOnClose: true, actions: react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => {
                onChange(selectedRow);
                onClose();
            } }, 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, type: !folderId ? 'application' : 'project', onScopeChange: (group) => {
                        setGroup(group);
                    } })),
            react_1.default.createElement(antd_1.Table, { bordered: true, size: "small", loading: loading, rowKey: (record) => record.contentId, columns: columns, dataSource: list, onChange: (pagination) => {
                    fetchList({
                        applicationId,
                        folderId: group === index_2.ScopeEnum.project ? folderId : undefined,
                        page: pagination.current || 1,
                        size: pagination.pageSize || 10,
                    });
                }, pagination: pageInfo.total > pageInfo.size
                    ? {
                        position: ['bottomCenter'],
                        current: pageInfo.page,
                        pageSize: pageInfo.size,
                        total: pageInfo.total,
                    }
                    : false, rowSelection: {
                    type: 'radio',
                    selectedRowKeys: selectedRow ? [selectedRow.contentId] : [],
                    onChange: (_selectedRowKeys, selectedRows) => {
                        setSelectedRow(selectedRows.length > 0 ? selectedRows[0] : undefined);
                    },
                } }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(ConditionSelect);
