"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get findDataByName () {
        return findDataByName;
    },
    get transformData () {
        return transformData;
    }
});
var generateId = function generateId(name) {
    return name.split('').reduce(function(sum, char) {
        return sum + char.charCodeAt(0);
    }, 0) // 使用字符的 Unicode 值作为 ID
    ;
};
var transformData = function transformData(data) {
    var groupByWordCode = function groupByWordCode1(data, parentId, parentName) {
        return data === null || data === void 0 ? void 0 : data.reduce(function(acc, item) {
            var wordCode = item.wordCode;
            if (!acc[wordCode]) {
                acc[wordCode] = [];
            }
            var currentId = generateId(item.value);
            acc[wordCode].push({
                name: item.value,
                wordCode: item.wordCode,
                id: currentId,
                pId: parentId,
                pName: parentName,
                children: item.children && groupByWordCode(item.children, currentId, item.text) || null
            });
            return acc;
        }, {});
    };
    var extractTitles = function extractTitles1(obj) {
        return Object.keys(obj).map(function(key) {
            return {
                title: key,
                list: obj[key].map(function(item) {
                    if (item.children) {
                        item.children = extractTitles(item.children);
                    }
                    return item;
                })
            };
        });
    };
    var middleData = groupByWordCode(data, '', '');
    var resultData = extractTitles(middleData);
    return resultData;
};
var findDataByName = function findDataByName1(data, name) {
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = data[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var item = _step.value;
            var _item_name;
            if (((_item_name = item.name) === null || _item_name === void 0 ? void 0 : _item_name.indexOf(name)) === 0) return item;
            if (item.children) {
                var found = findDataByName(item.children, name);
                if (found) return found;
            }
            if (item.list) {
                var found1 = findDataByName(item.list, name);
                if (found1) return found1;
            }
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
    return null // 如果没有找到，返回 null
    ;
};
