function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import { ensureNodeWithId } from "./handlers";
import { fileIdOrTreeKeyToRelativePath } from "./workspaceFileId";
var lookupFlatFileNode = function lookupFlatFileNode(node, rawId, index) {
    var _node_file;
    var candidates = [
        fileIdOrTreeKeyToRelativePath(rawId),
        fileIdOrTreeKeyToRelativePath(node.key),
        ((_node_file = node.file) === null || _node_file === void 0 ? void 0 : _node_file.id) ? fileIdOrTreeKeyToRelativePath(node.file.id) : null,
        node.id ? fileIdOrTreeKeyToRelativePath(String(node.id)) : null
    ];
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = candidates[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var relativePath = _step.value;
            if (!relativePath) {
                continue;
            }
            var matched = index.get(relativePath);
            if (matched) {
                return matched;
            }
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
    return undefined;
};
/**
 * 将文件树叶子节点解析为与平铺列表一致的 {@link FileNode}
 * @description 合并 `file` 与节点顶层的 url/content 等字段；若提供 {@link ResolveTreeLeafFileOptions.fileNodeByRelativePath}，则按相对路径对齐列表节点（统一 `workspace:` / `file:` 等 id 前缀）
 */ export var resolveTreeLeafFile = function resolveTreeLeafFile(node, options) {
    var _node_isLeaf, _ref, _ref1, _ref2;
    var hasChildren = Boolean(node.children && node.children.length > 0);
    var resolvedIsLeaf = (_node_isLeaf = node.isLeaf) !== null && _node_isLeaf !== void 0 ? _node_isLeaf : !hasChildren;
    if (!resolvedIsLeaf) {
        return null;
    }
    var key = node.key, name = node.name, _children = node.children, _isLeaf = node.isLeaf, _icon = node.icon, _treeDisabled = node.disabled, id = node.id, file = node.file, nodeFileFields = _object_without_properties(node, [
        "key",
        "name",
        "children",
        "isLeaf",
        "icon",
        "disabled",
        "id",
        "file"
    ]);
    var rawId = (_ref = (_ref1 = file === null || file === void 0 ? void 0 : file.id) !== null && _ref1 !== void 0 ? _ref1 : id !== undefined ? String(id) : undefined) !== null && _ref !== void 0 ? _ref : key;
    var base = ensureNodeWithId(_object_spread_props(_object_spread({}, nodeFileFields, file !== null && file !== void 0 ? file : {}), {
        name: (_ref2 = file === null || file === void 0 ? void 0 : file.name) !== null && _ref2 !== void 0 ? _ref2 : name,
        id: rawId
    }));
    var index = options === null || options === void 0 ? void 0 : options.fileNodeByRelativePath;
    if (!(index === null || index === void 0 ? void 0 : index.size)) {
        return base;
    }
    var matched = lookupFlatFileNode(node, rawId, index);
    if (!matched) {
        return base;
    }
    return ensureNodeWithId(_object_spread_props(_object_spread({}, matched, base), {
        id: matched.id,
        name: base.name
    }));
};
