"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_router_dom_1 = require("react-router-dom");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const index_1 = require("@/components/index");
const global_1 = require("@/constants/global");
const system_1 = require("@/pages/system");
const period_format_1 = require("@/utils/period-format");
const ContentList = (props) => {
    const { applicationId, contents, fileDetail, folderId, loading } = props;
    const [extendRecord, setExtendRecord] = (0, react_1.useState)();
    const [list, setList] = (0, react_1.useState)();
    const { pathname, search } = (0, react_router_dom_1.useLocation)();
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { content, global, version } = locale.business;
    // re-sort by inherit relation
    (0, react_1.useEffect)(() => {
        let newList = [];
        let extendRecord = {};
        if ((fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.type) === global_1.FileType.page) {
            const contentRecord = {};
            contents.forEach((item) => {
                if (item.isBase || !item.extendId) {
                    newList.push(item);
                }
                else {
                    if (!contentRecord[item.extendId]) {
                        contentRecord[item.extendId] = [];
                    }
                    contentRecord[item.extendId].push(item);
                    if (!extendRecord[item.extendId]) {
                        extendRecord[item.extendId] = [];
                    }
                    extendRecord[item.extendId].push(item.id);
                }
            });
            Object.keys(contentRecord).forEach((key) => {
                const children = contentRecord[key];
                if (children && children.length > 0) {
                    const idx = newList.findIndex((item) => item.id === key);
                    newList.splice(idx + 1, 0, ...children);
                }
            });
        }
        else {
            newList = contents;
        }
        setList(newList);
        setExtendRecord(extendRecord);
    }, [contents, fileDetail]);
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'title',
            render: (text, record) => {
                var _a;
                return (react_1.default.createElement(index_1.NameContainer, null,
                    react_1.default.createElement(react_router_dom_1.Link, { to: {
                            pathname: '/builder',
                            search: `?applicationId=${applicationId}&folderId=${folderId}&fileId=${record.fileId}&contentId=${record.id}`,
                            state: { backPathname: pathname, backSearch: search },
                        } },
                        react_1.default.createElement(antd_1.Tooltip, { placement: "topLeft", mouseEnterDelay: 1, title: text },
                            react_1.default.createElement(index_1.Name, null, text))),
                    record.urls && !record.isBase && record.urls.length > 0 && (react_1.default.createElement(antd_1.Popover, { placement: "bottom", content: react_1.default.createElement(react_1.default.Fragment, null, (_a = record.urls) === null || _a === void 0 ? void 0 : _a.map((url) => {
                            return (react_1.default.createElement("p", { key: url },
                                react_1.default.createElement("a", { href: url, target: "_blank" }, url)));
                        })), trigger: "hover" },
                        react_1.default.createElement(icons_1.LinkOutlined, { style: { marginLeft: 6 } })))));
            },
        },
        {
            title: global.type,
            dataIndex: 'version',
            key: 'version',
            render: (_text) => {
                return react_1.default.createElement(antd_1.Tag, { color: "orange" }, content.name);
            },
        },
        {
            title: version.liveVersion,
            dataIndex: 'version',
            key: 'version',
            render: (text) => {
                return text || '--';
            },
        },
        {
            title: global.locale,
            dataIndex: 'tag',
            key: 'tag',
            render: (_text, record) => {
                if (record.isBase) {
                    return react_1.default.createElement(index_1.LocaleTag, { color: "blue" }, "base");
                }
                const localesTag = record.tags ? record.tags.filter((item) => item.locale) : [];
                return react_1.default.createElement(index_1.LocaleView, { locales: localesTag.map((item) => item.locale) });
            },
        },
        {
            title: global.creator,
            dataIndex: 'creator',
            key: 'creator',
            render: (_text, record) => {
                return record.creator ? record.creator.account : '--';
            },
        },
        {
            title: global.createTime,
            dataIndex: 'createTime',
            key: 'createTime',
            width: 200,
            render: (text) => (0, period_format_1.periodFormat)(text, 'unknown'),
        },
    ];
    // inherit tag column
    if ((fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.type) === global_1.FileType.page) {
        columns.unshift({
            title: '',
            key: '',
            width: 20,
            render: (_text, record) => {
                var _a;
                if (record.isBase && ((_a = extendRecord[record.id]) === null || _a === void 0 ? void 0 : _a.length)) {
                    return react_1.default.createElement(index_1.BasicTemRing, null);
                }
                if (record.extendId) {
                    return (react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement(index_1.Ring, null),
                        react_1.default.createElement(index_1.VLine, null)));
                }
                return react_1.default.createElement(react_1.default.Fragment, null);
            },
        });
    }
    return react_1.default.createElement(antd_1.Table, { rowKey: "id", loading: loading, pagination: false, columns: columns, dataSource: list });
};
exports.default = ContentList;
