"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 icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const ACTIONS = __importStar(require("@/actions/applications/detail/file/variables"));
const index_1 = require("@/components/index");
const system_1 = require("@/pages/system");
const index_2 = require("@/utils/index");
const Table = (0, styled_components_1.default)(antd_1.Table) `
  .ant-table-pagination.ant-pagination {
    margin: 24px 0 0;
  }
`;
const mapStateToProps = (store) => ({
    applicationId: store.applications.detail.settings.app.applicationId,
    loading: store.applications.detail.file.variables.loading,
    pageInfo: store.applications.detail.file.variables.pageInfo,
    list: store.applications.detail.file.variables.list,
});
const mapDispatchToProps = {
    fetchList: ACTIONS.fetchList,
    fetchBuildVersion: ACTIONS.fetchVariableBuilderVersion,
    openEditDrawer: ACTIONS.openEditDrawer,
    saveVariable: ACTIONS.saveVariable,
    publishVariable: ACTIONS.publishVariable,
    commitToStore: ACTIONS.commitToStore,
    deleteVariable: ACTIONS.deleteVariable,
};
function ConditionList(props) {
    const { applicationId, loading, pageInfo, list, fetchList, fetchBuildVersion, publishVariable, commitToStore, deleteVariable, openEditDrawer, } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { folder, global, store } = locale.business;
    const handleFetchList = () => {
        fetchList({ applicationId, page: pageInfo.page, size: pageInfo.size });
    };
    const handleDelete = (id) => {
        if (applicationId && id) {
            deleteVariable({
                applicationId,
                id,
                status: true,
            }, () => {
                fetchList({
                    applicationId,
                    page: pageInfo.page,
                    size: pageInfo.size,
                });
            });
        }
    };
    const handlePublish = (contentId) => {
        if (contentId) {
            publishVariable({
                applicationId,
                contentId,
                status: 'release',
            }, handleFetchList);
        }
    };
    const handleCommit = (id, isOnline) => {
        if (id) {
            commitToStore({
                id,
                applicationId: applicationId,
                type: 'variable',
                isOnline,
            }, handleFetchList);
        }
    };
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: folder.name,
            dataIndex: 'folderName',
            key: 'folderName',
            render: (folderName) => folderName === '_variable' ? react_1.default.createElement(antd_1.Tag, { color: "blue" }, global.application) : react_1.default.createElement("span", null, folderName),
        },
        {
            title: global.type,
            dataIndex: 'type',
            key: 'schemas',
            render: (_text, record) => {
                var _a;
                const schemas = (_a = record.content) === null || _a === void 0 ? void 0 : _a.schemas;
                return react_1.default.createElement(react_1.default.Fragment, null, (schemas === null || schemas === void 0 ? void 0 : schemas.length) > 0 ? schemas[0].type : '--');
            },
        },
        {
            title: global.creator,
            dataIndex: 'creator',
            key: 'creator',
            render: (creator) => (creator === null || creator === void 0 ? void 0 : creator.account) || '',
        },
        {
            title: global.createTime,
            dataIndex: 'createTime',
            key: 'createTime',
            width: 200,
            render: (text) => (0, index_2.periodFormat)(text, 'unknown'),
        },
        {
            title: global.actions,
            key: 'id',
            width: 160,
            render: (_text, record) => {
                return (react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: "Edit", onClick: () => {
                            openEditDrawer(true, record, 'edit');
                            fetchBuildVersion(applicationId, record);
                        }, style: { marginRight: 8 } },
                        react_1.default.createElement(icons_1.EditOutlined, null)),
                    react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: global.publish, onClick: () => handlePublish(record.contentId), style: { marginRight: 8 } },
                        react_1.default.createElement(index_1.PublishIcon, null)),
                    react_1.default.createElement(antd_1.Popconfirm, { title: (record === null || record === void 0 ? void 0 : record.online) ? store.revokeTitle : store.commitTitle, okText: global.yes, cancelText: global.no, onConfirm: () => handleCommit(record.id, record.online) },
                        react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: (record === null || record === void 0 ? void 0 : record.online) ? store.revoke : store.commit, style: { marginRight: 8 } }, (record === null || record === void 0 ? void 0 : record.online) ? react_1.default.createElement(icons_1.ArrowDownOutlined, null) : react_1.default.createElement(icons_1.ArrowUpOutlined, null))),
                    react_1.default.createElement(antd_1.Popconfirm, { title: `${global.deleteMsg} ${record.name}?`, onConfirm: () => {
                            handleDelete(record.id);
                        }, okText: global.yes, cancelText: global.no },
                        react_1.default.createElement(antd_1.Button, { size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.DeleteOutlined, null) }))));
            },
        },
    ];
    return (react_1.default.createElement(Table, { rowKey: "id", loading: loading, columns: columns, dataSource: list, 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)(ConditionList);
