export const findId = (array: [], id: any) => {
    let result = false;
    result = array.includes(id);
    return result;
}

// 判断是字符串数组还是number数组
export const isArrayOfType = (arr: [], type: 'string' | 'number'): boolean => {
    return arr.every(item => typeof item === type);
}


export function findAncestors(data: any, targetId: number): number[] {
    const ancestors: number[] = [];

    function traverse(node: any): boolean {
        if (node.id === targetId) {
            return true;
        }

        if (node.children) {
            for (const child of node.children) {
                if (traverse(child)) {
                    ancestors.unshift(node.id);
                    return true;
                }
            }
        }

        return false;
    }

    for (const node of data) {
        traverse(node);
    }

    return ancestors;
}

export function  findAncestorsHanzi (data: any, nativePlaceTxt: string): any  {
    const result: any = [];

    const findAncestors = (list: any, targetName: string, path: any = []): any => {
        for (const item of list) {
            if (item.name === targetName) {
                return [...path, item];
            }
            if (item.children) {
                const foundPath = findAncestors(item.children, targetName, [...path, item]);
                if (foundPath.length > 0) {
                    return foundPath;
                }
            }
        }
        return [];
    };

    const ancestors = findAncestors(data, nativePlaceTxt);
    return ancestors;
};


export function findItemById(arr: any[], id: number): any | false {
    for (const item of arr) {
        if (item.id === id) {
            return true;
        }
        if (item.children && Array.isArray(item.children)) {
            const result = findItemById(item.children, id);
            if (result) {
                return true;
            }
        }
    }
    return false;
}