"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 });
exports.ComponentResource = void 0;
const react_1 = __importStar(require("react"));
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const lodash_1 = __importDefault(require("lodash"));
const widgets_1 = require("@/components/widgets");
const system_1 = require("@/pages/system");
require("../style.css");
const { TreeNode } = antd_1.TreeSelect;
const ComponentResource = (props) => {
    const { componentRemote, lastVersion, onChange } = props;
    const [mappingRelation, setMappingRelation] = (0, react_1.useState)({});
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { version: versionI18n, resource } = locale.business;
    (0, react_1.useEffect)(() => {
        if (componentRemote === null || componentRemote === void 0 ? void 0 : componentRemote.component) {
            const { content: { resource: { entry: { node, css, browser, debug, editor }, }, }, } = componentRemote.component;
            setMappingRelation({
                node: getShortPath(node === null || node === void 0 ? void 0 : node.path),
                browser: getShortPath(browser === null || browser === void 0 ? void 0 : browser.path),
                debug: getShortPath(debug === null || debug === void 0 ? void 0 : debug.path),
                css: getShortPath(css === null || css === void 0 ? void 0 : css.path),
                editor: getShortPath(editor === null || editor === void 0 ? void 0 : editor.path),
            });
        }
    }, [componentRemote]);
    (0, react_1.useEffect)(() => {
        if (typeof onChange === 'function') {
            onChange(mappingRelation);
        }
    }, [mappingRelation]);
    const handleUpdateComponent = (key, value) => {
        const newMapping = Object.assign({}, mappingRelation);
        newMapping[key] = value;
        setMappingRelation(newMapping);
    };
    const getShortPath = (path) => {
        if (!path) {
            return '';
        }
        const array = path.split('/');
        return array.splice(array.length - 2, array.length).join('/');
    };
    const treeNode = (0, react_1.useMemo)(() => {
        if (!componentRemote) {
            return null;
        }
        const { resource: { files }, } = componentRemote;
        return (react_1.default.createElement(react_1.default.Fragment, null, Object.entries(files).map(([key, value]) => {
            const isLeaf = !lodash_1.default.isObject(value);
            return (react_1.default.createElement(TreeNode, { key: `${key}`, value: key, title: key, isLeaf: isLeaf, selectable: isLeaf }, !isLeaf &&
                Object.keys(value).map((subKey) => {
                    return (react_1.default.createElement(TreeNode, { key: `${key}/${subKey}`, value: `${key}/${subKey}`, title: subKey, isLeaf: true }));
                })));
        })));
    }, [componentRemote]);
    const treeSelectProps = {
        style: { width: '100%' },
        dropdownStyle: { maxHeight: 400, overflow: 'auto' },
        treeDefaultExpandAll: true,
        allowClear: true,
    };
    const componentRemoteNode = (0, react_1.useMemo)(() => {
        if (!componentRemote) {
            return null;
        }
        const { resource: { files, latestVersion, version, resourceName }, component: { version: componentNewVersion }, } = componentRemote;
        return (react_1.default.createElement(widgets_1.Group, null,
            react_1.default.createElement(widgets_1.Title, null, "Manifest"),
            react_1.default.createElement("table", { className: "source-diff-table" },
                react_1.default.createElement("tbody", null,
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", null, versionI18n.componentVersion),
                        react_1.default.createElement("td", { className: "after" }, version)),
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", null, resource.resourceVersion),
                        react_1.default.createElement("td", { className: "after" }, resourceName)),
                    Object.entries(files).map(([key, value]) => {
                        if (lodash_1.default.isObject(value)) {
                            return Object.entries(value)
                                .map(([subKey, subValue]) => {
                                return (react_1.default.createElement("tr", { key: subKey },
                                    react_1.default.createElement("td", null,
                                        key,
                                        "/",
                                        subKey),
                                    react_1.default.createElement("td", { className: "after" }, subValue)));
                            })
                                .flat();
                        }
                        else {
                            return (react_1.default.createElement("tr", { key: key },
                                react_1.default.createElement("td", null, key),
                                react_1.default.createElement("td", { className: "after" }, value)));
                        }
                    }))),
            react_1.default.createElement(widgets_1.Title, null, resource.resourceVersion),
            react_1.default.createElement("table", { className: "source-diff-table" },
                react_1.default.createElement("tbody", null,
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", { className: "before" }, latestVersion || '-'),
                        react_1.default.createElement("td", { style: { textAlign: 'center' } },
                            react_1.default.createElement(icons_1.SwapRightOutlined, null)),
                        react_1.default.createElement("td", { className: "after" }, version)))),
            react_1.default.createElement(widgets_1.Title, null, versionI18n.componentVersion),
            react_1.default.createElement("table", { className: "source-diff-table" },
                react_1.default.createElement("tbody", null,
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", { className: "before" }, (lastVersion === null || lastVersion === void 0 ? void 0 : lastVersion.version) || '-'),
                        react_1.default.createElement("td", { style: { textAlign: 'center' } },
                            react_1.default.createElement(icons_1.SwapRightOutlined, null)),
                        react_1.default.createElement("td", { className: "after" }, componentNewVersion)))),
            react_1.default.createElement(widgets_1.Title, null, "Mapping"),
            react_1.default.createElement("table", { className: "source-diff-table" },
                react_1.default.createElement("tbody", null,
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", null, "Node"),
                        react_1.default.createElement("td", { className: "after", width: "70%" },
                            react_1.default.createElement(antd_1.TreeSelect, Object.assign({}, treeSelectProps, { value: mappingRelation.node, onChange: (value) => {
                                    handleUpdateComponent('node', value);
                                } }), treeNode))),
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", null, "Browser"),
                        react_1.default.createElement("td", { className: "after", width: "70%" },
                            react_1.default.createElement(antd_1.TreeSelect, Object.assign({}, treeSelectProps, { value: mappingRelation.browser, onChange: (value) => {
                                    handleUpdateComponent('browser', value);
                                } }), treeNode))),
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", null, "Debug"),
                        react_1.default.createElement("td", { className: "after", width: "70%" },
                            react_1.default.createElement(antd_1.TreeSelect, Object.assign({}, treeSelectProps, { value: mappingRelation.debug, onChange: (value) => {
                                    handleUpdateComponent('debug', value);
                                } }), treeNode))),
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", null, "Css"),
                        react_1.default.createElement("td", { className: "after", width: "70%" },
                            react_1.default.createElement(antd_1.TreeSelect, Object.assign({}, treeSelectProps, { value: mappingRelation.css, onChange: (value) => {
                                    handleUpdateComponent('css', value);
                                } }), treeNode))),
                    react_1.default.createElement("tr", null,
                        react_1.default.createElement("td", null, "Editor"),
                        react_1.default.createElement("td", { className: "after", width: "70%" },
                            react_1.default.createElement(antd_1.TreeSelect, Object.assign({}, treeSelectProps, { value: mappingRelation.editor, onChange: (value) => {
                                    handleUpdateComponent('editor', value);
                                } }), treeNode)))))));
    }, [componentRemote, treeNode, mappingRelation]);
    return componentRemoteNode;
};
exports.ComponentResource = ComponentResource;
exports.default = exports.ComponentResource;
