"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const antd_1 = require("antd");
const application_1 = require("@/apis/application");
const ResPathTreeSelect = (props) => {
    const { applicationId, value, disabled, onChange } = props;
    const [treeData, setTreeData] = (0, react_1.useState)([]);
    const [treeValue, setTreeValue] = (0, react_1.useState)(value);
    (0, react_1.useEffect)(() => {
        (() => __awaiter(void 0, void 0, void 0, function* () {
            if (!applicationId) {
                console.warn('applicationId is empty! can\'t init resource path tree select');
                return;
            }
            const nodeList = yield (0, application_1.getResourcesGroupSearchsWithCache)({
                applicationId,
            }).then((list = []) => {
                return list.map(({ id, name, folderPath }) => ({
                    id: id,
                    pId: undefined,
                    value: id,
                    title: name,
                    path: folderPath,
                    isLeaf: false,
                    selectable: false,
                }));
            });
            setTreeData(nodeList);
        }))();
    }, [applicationId]);
    (0, react_1.useEffect)(() => {
        if (value && (treeValue === null || treeValue === void 0 ? void 0 : treeValue.value) !== (value === null || value === void 0 ? void 0 : value.value)) {
            if (treeData.find((data) => data.value === value.value)) {
                setTreeValue(value);
            }
            else {
                setTreeValue(Object.assign(Object.assign({}, value), { value: value.label }));
            }
        }
    }, [value, treeData]);
    const handleChange = (selectValue) => {
        setTreeValue(selectValue);
        if (typeof onChange === 'function')
            onChange(selectValue);
    };
    const onLoadData = (dataNode) => {
        const { id: pId, path } = dataNode;
        return (0, application_1.getResourcesByPathsWithCache)({
            applicationId,
            path,
        }).then((data) => {
            const { children } = data;
            const { folders = [], files = [] } = children || {};
            const res = [
                ...folders.map(({ id, name, folderPath }) => ({
                    id: id,
                    pId,
                    value: id,
                    title: name,
                    path: `${path}/${folderPath}`,
                    isLeaf: false,
                    selectable: false,
                })),
                ...files.map(({ contentId, name }) => ({
                    id: contentId,
                    pId,
                    value: contentId,
                    title: name,
                    path: `${path}/${name}`,
                    isLeaf: true,
                    selectable: true,
                })),
            ];
            setTreeData((_treeData) => _treeData.concat(res));
        });
    };
    return (react_1.default.createElement(antd_1.TreeSelect, { treeDataSimpleMode: true, labelInValue: true, allowClear: true, disabled: disabled, style: { width: '100%' }, value: treeValue, dropdownStyle: { maxHeight: 400, overflow: 'auto' }, placeholder: "Please select", onChange: handleChange, loadData: onLoadData, treeData: treeData, treeNodeLabelProp: "path" }));
};
exports.default = ResPathTreeSelect;
