"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 icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const condition_1 = require("@/constants/condition");
const system_1 = require("@/pages/system");
const Main_1 = require("../../Main");
var TabEnum;
(function (TabEnum) {
    TabEnum[TabEnum["project"] = 0] = "project";
    TabEnum[TabEnum["application"] = 1] = "application";
})(TabEnum || (TabEnum = {}));
const Container = styled_components_1.default.div `
  padding: 24px 24px 0;
`;
const Header = styled_components_1.default.div `
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
`;
const Table = (0, styled_components_1.default)(antd_1.Table) `
  .ant-table-pagination.ant-pagination {
    margin: 12px 0 0;
  }
`;
const Main = (props) => {
    const { type, applicationId, folderId, button, paginationInfo, dataSource, onDelete, onEdit, onFetch, } = props;
    const [tab, setTab] = (0, react_1.useState)(TabEnum.project);
    const [search, setSearch] = (0, react_1.useState)('');
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global } = locale.business;
    // radio options
    const radioOptions = [
        {
            label: global.project,
            value: 0,
        },
        {
            label: global.application,
            value: 1,
        },
    ];
    const pageInfo = (0, react_1.useMemo)(() => paginationInfo[type || 'condition'], [type, paginationInfo]);
    const params = (0, react_1.useMemo)(() => {
        let params = {
            applicationId,
            page: pageInfo === null || pageInfo === void 0 ? void 0 : pageInfo.page,
            size: pageInfo === null || pageInfo === void 0 ? void 0 : pageInfo.size,
            search,
        };
        if (tab === TabEnum.project) {
            params = Object.assign(Object.assign({}, params), { folderId });
        }
        else {
            params = Object.assign(Object.assign({}, params), { type: 'live' });
        }
        return params;
    }, [applicationId, folderId, pageInfo, tab, search]);
    const handleFetchList = (0, react_1.useCallback)((page, size) => {
        const _params = Object.assign(Object.assign({}, params), { page: page || params.page, size: size || params.size, search });
        if (typeof onFetch === 'function')
            onFetch(_params);
    }, [params]);
    (0, react_1.useEffect)(() => {
        handleFetchList();
    }, [tab, search]);
    const handleGenerateType = (0, react_1.useCallback)((entity) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
        let entityType = '';
        if (type === Main_1.ModalTypeEnum.condition) {
            entityType = condition_1.ConditionTypeEnum[((_c = (_b = (_a = entity.content) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.type) || 0];
        }
        if (type === Main_1.ModalTypeEnum.function) {
            entityType = ((_g = (_f = (_e = (_d = entity.content) === null || _d === void 0 ? void 0 : _d.schemas) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.props) === null || _g === void 0 ? void 0 : _g.async) ? 'async' : 'sync';
        }
        if (type === Main_1.ModalTypeEnum.variable) {
            entityType = ((_k = (_j = (_h = entity.content) === null || _h === void 0 ? void 0 : _h.schemas) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.type) || '--';
        }
        return entityType;
    }, [type]);
    const handleDelete = (id) => {
        if (id && typeof onDelete === 'function')
            onDelete(id, params);
    };
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: global.type,
            dataIndex: 'type',
            key: 'type',
            width: 150,
            render: (_, record) => handleGenerateType(record),
        },
        {
            title: global.actions,
            dataIndex: '',
            key: '',
            width: 100,
            render: (_text, record) => (react_1.default.createElement(react_1.default.Fragment, null, tab === TabEnum.project ? (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(antd_1.Button, { size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.EditOutlined, null), onClick: () => onEdit('edit', record), style: { marginRight: 8 } }),
                react_1.default.createElement(antd_1.Popconfirm, { title: `${global.deleteMsg}${record.name}?`, okText: global.yes, cancelText: global.no, onConfirm: () => handleDelete(record.id) },
                    react_1.default.createElement(antd_1.Button, { size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.DeleteOutlined, null) })))) : (react_1.default.createElement(antd_1.Button, { size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.EyeOutlined, null), onClick: () => onEdit('view', record) })))),
        },
    ];
    return (react_1.default.createElement(Container, null,
        react_1.default.createElement(Header, null,
            react_1.default.createElement(antd_1.Radio.Group, { size: "small", optionType: "button", options: radioOptions, value: tab, onChange: (e) => setTab(e.target.value) }),
            react_1.default.createElement("div", null,
                react_1.default.createElement(antd_1.Input, { allowClear: true, size: "small", placeholder: global.inputSearchText, value: search, onChange: (e) => setSearch(e.target.value), style: { width: 200, marginRight: 8 } }),
                tab === TabEnum.project && (react_1.default.createElement(antd_1.Button, { type: "primary", size: "small", icon: react_1.default.createElement(icons_1.PlusOutlined, null), onClick: () => onEdit('new') }, button)))),
        dataSource && (react_1.default.createElement(Table, { bordered: false, rowKey: (record) => record.contentId, columns: columns, dataSource: dataSource, pagination: pageInfo.total > pageInfo.size
                ? {
                    position: ['bottomCenter'],
                    current: pageInfo.page,
                    pageSize: pageInfo.size,
                    total: pageInfo.total,
                }
                : false, onChange: (pagination) => {
                handleFetchList(pagination.current, pagination.pageSize);
            } }))));
};
exports.default = Main;
