"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;
};
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 ACTIONS = __importStar(require("@/actions/applications/detail/resources/detail"));
const index_1 = require("@/components/index");
const system_1 = require("@/pages/system");
const mapStateToProps = (store) => ({
    applicationId: store.applications.detail.settings.app.applicationId,
    resLoading: store.applications.detail.resources.detail.resLoading,
    folderPath: store.applications.detail.resources.detail.folderPath,
    resourceList: store.applications.detail.resources.detail.resourceList,
    selectedRowKeys: store.applications.detail.resources.detail.selectedRowKeys,
});
const mapDispatchToProps = {
    fetchResourceData: ACTIONS.fetchResourcesList,
    updateSelectedRowKeys: ACTIONS.updateSelectedRowKeys,
    editorFile: ACTIONS.updateResourcesDetailFileDrawerState,
    editorFolder: ACTIONS.updateResourcesDetailFolderDrawerState,
    removeResource: ACTIONS.removeResources,
};
const List = (props) => {
    const { applicationId, resLoading = false, selectedRowKeys, resourceList = [], folderPath, fetchResourceData, updateSelectedRowKeys, editorFile, editorFolder, removeResource, } = props;
    const isInitRef = (0, react_1.useRef)(false);
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, file, folder } = locale.business;
    // search params
    const history = (0, react_router_dom_1.useHistory)();
    const { location } = history;
    (0, react_1.useEffect)(() => {
        if (!folderPath)
            return;
        if (applicationId) {
            fetchResourceData({
                applicationId,
                folderPath,
            });
            isInitRef.current = true;
        }
    }, [folderPath, applicationId]);
    const columns = [
        {
            title: '',
            dataIndex: '__type',
            width: 40,
            render: (__type) => (react_1.default.createElement("span", null,
                __type === 'file' && react_1.default.createElement(icons_1.FileTextOutlined, null),
                __type === 'folder' && react_1.default.createElement(icons_1.FolderOpenOutlined, null))),
        },
        {
            title: global.nameLabel,
            dataIndex: 'name',
            key: 'name',
            render: (name, record) => {
                const { __type, folderPath } = record;
                if (__type === 'file') {
                    return name;
                }
                const href = history.createHref(Object.assign(Object.assign({}, location), { pathname: `${location.pathname.replace(/\/$/, '')}/${folderPath}` }));
                return react_1.default.createElement("a", { href: href }, name);
            },
        },
        {
            title: file.relPath,
            dataIndex: 'content',
            key: 'content',
            render: (content, record) => {
                const { __type } = record;
                if (__type !== 'file') {
                    return null;
                }
                return (content === null || content === void 0 ? void 0 : content.realPath) || '';
            },
        },
        {
            title: global.actions,
            width: '200px',
            align: 'right',
            key: 'actions',
            render: (record) => (react_1.default.createElement("span", null,
                react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.EditOutlined, null), style: { marginLeft: 8 }, onClick: () => handleEdit(record) }),
                react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.DeleteOutlined, null), style: { marginLeft: 8 }, onClick: () => handleDelete(record) }))),
        },
    ];
    const handleEdit = (record) => {
        const { __type, id, name, content } = record;
        if (__type === 'file') {
            editorFile({
                open: true,
                type: 'edit',
                data: {
                    id,
                    filepath: (content === null || content === void 0 ? void 0 : content.realPath) || '',
                },
            });
        }
        else if (__type === 'folder') {
            editorFolder({
                open: true,
                type: 'edit',
                data: {
                    id,
                    name: name || '',
                },
            });
        }
    };
    const handleDelete = (record) => {
        const { id } = record;
        antd_1.Modal.confirm({
            title: folder.deleteTitle,
            content: folder.deleteMsg,
            okText: global.yes,
            okType: 'danger',
            cancelText: global.no,
            onOk() {
                removeResource({
                    applicationId,
                    ids: [id],
                });
            },
        });
    };
    const onSelectChange = (_selectedRowKeys) => {
        updateSelectedRowKeys(_selectedRowKeys);
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(index_1.FoxPageTable, { rowKey: "id", scroll: undefined, pagination: false, loading: !isInitRef.current || resLoading, columns: columns, rowSelection: {
                selectedRowKeys,
                onChange: onSelectChange,
            }, dataSource: resourceList })));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(List);
