/**
 *
 * @param id 最后一级的id
 * @returns 数组，所有父id+自己id的数组
 */

import { A5TableType } from "@/types";

const lastIdresArrFu = (id: string, arrAll: A5TableType[], flag?: boolean) => {
  let arr: string[] = [];

  let str: string = "";

  arrAll.forEach((v1) => {
    if (v1.id === id) {
      arr = [id];
      str = v1.name;
    } else {
      v1.children?.forEach((v2) => {
        if (v2.id === id) {
          arr = [v1.id, id];
          str = v1.name + " / " + v2.name;
        } else {
          v2.children?.forEach((v3) => {
            if (v3.id === id) {
              arr = [v1.id, v2.id, id];
              str = v1.name + " / " + v2.name + " / " + v3.name;
            } else {
              v3.children?.forEach((v4) => {
                if (v4.id === id) {
                  arr = [v1.id, v2.id, v3.id, id];
                  str =
                    v1.name +
                    " / " +
                    v2.name +
                    " / " +
                    v3.name +
                    " / " +
                    v4.name;
                }
              });
            }
          });
        }
      });
    }
  });

  return flag ? str : arr;
};

export default lastIdresArrFu;
