import {
  SceneListType,
  GetListWorkshop,
  WorkspaceAddDatas,
  LabelModuleListType,
} from '@/api/scene/sceneOperate';
import { WorkShopTempleteType } from '@/api/scene/secene-templet';
import { useGlobSetting } from '@/hooks/setting';

const { appPCUrl } = useGlobSetting()

export const colomns = [
  { label: '场景名称', prop: 'name', width: 300 },
  { label: '场景标签', prop: 'labelName', width: 300 },
  { label: '代码', prop: 'code' },
  {
    label: '预览',
    prop: 'preview',
    render: ({ row, column }) => {
      if (!row.parent && row.labelList?.[0]?.id) {
        return (
          <a
            href={`${appPCUrl}#/company?companyId=${row.id}&labelId=${row.labelList[0]?.id}`}
            target="_blank"
          >
            公司预览
          </a>
        );
      } else if (row.parent && !row.parent.parent) {
        return (
          <a href={`${appPCUrl}#/shop?id=${row.id}`} target="_blank">
            车间预览
          </a>
        );
      }
    },
  },
];

interface DataSourceUser
  extends SceneListType<
  GetListWorkshop<WorkspaceAddDatas, WorkShopTempleteType>,
  LabelModuleListType
  > {
  parent?: SceneListType<
    GetListWorkshop<WorkspaceAddDatas, WorkShopTempleteType>,
    LabelModuleListType
  > | null;
}

export const dataSourceWithParent = (d: DataSourceUser[], parent: DataSourceUser | null) => {
  d.forEach((item, _index) => {
    item.parent = parent;

    if (item.children && item.children?.length > 0) {
      dataSourceWithParent(item.children, item);
    }
  });

  return d;
};

export const options = [
  {
    value: 'industry',
    label: '工业企业',
  },
  {
    value: 'gas',
    label: '燃气企业',
  },
  {
    value: 'other',
    label: '其他企业',
  },
];

export function removeParent(data) {
  return data.map((item) => {
    // 删除 tag 属性
    delete item.parent;

    // 递归处理子项
    if (item.children && item.children.length > 0) {
      item.children = removeParent(item.children);
    }

    return item;
  });
}

//找到各个层级
// export function findIndexByItem(data, targetItem, path = []) {
//   for (let i = 0; i < data.length; i++) {
//     const currentPath = path.concat(i);
//     if (data[i].id === targetItem.id && data[i].name === targetItem.name) {
//       return currentPath;
//     }
//     if (data[i].children && data[i].children.length > 0) {
//       const childResult = findIndexByItem(data[i].children, targetItem, currentPath);
//       if (childResult.length > 0) {
//         return childResult;
//       }
//     }
//   }
//   return [];
// }

//用于重新修改serial
export const updateSerials = (data) => {
  for (let i = 0; i < data.length; i++) {
    data[i].serial = i;

    // 如果有子项，递归查找
    if (data[i].children && data[i].children.length > 0) {
      updateSerials(data[i].children);
    }
  }
  return data;
  // return false; // 表示未找到目标项
};

//用于新增数据
export const updateData = (data, targetId, newAdd) => {
  for (let i = 0; i < data.length; i++) {
    const currentItem = data[i];

    if (currentItem.id === targetId) {
      if (!currentItem.children) {
        currentItem.children = [];
      }
      currentItem.children.push(newAdd);
      return true; // 表示已经找到并修改
    }

    // 如果有子项，递归查找
    if (currentItem.children && currentItem.children.length > 0) {
      const found = updateData(currentItem.children, targetId, newAdd);

      if (found) {
        return true; // 如果在子项中找到目标项，停止继续查找
      }
    }
  }

  return false; // 表示未找到目标项
};

//判断该条数据的层级
export const findItemLevel = (data, targetId, targetName, currentLevel = 0) => {
  for (let i = 0; i < data.length; i++) {
    const item = data[i];

    if (item.id === targetId && item.name === targetName) {
      return currentLevel;
    }

    if (item.children && item.children.length > 0) {
      const childLevel = findItemLevel(item.children, targetId, targetName, currentLevel + 1);
      if (childLevel !== -1) {
        return childLevel;
      }
    }
  }

  return -1;
};


//得到parent
export const getParent = (data: DataSourceUser[], targetCode: string) => {


  //let parentNode = {} as DataSourceUser
  for (let i = 0; i < data.length; i++) {
    const item = data[i];

    if (item.code === targetCode) {
      // parentNode = item
      return item
    }

    if (item.children && item.children.length > 0) {
      const foundItem = getParent(item.children, targetCode);
      if (foundItem) {
        return foundItem
      }
    }
  }

  return null;
};

//删除行
export const deleteTableRow = (dataSource, targetId) => {
  const deleteRecursive = (data) => {
    for (let i = 0; i < data.length; i++) {
      const currentItem = data[i];

      if (currentItem.id === targetId) {
        // 删除当前项
        data.splice(i, 1);
        return true;
      }

      // 如果有子项，递归查找
      if (currentItem.children && currentItem.children.length > 0) {
        const found = deleteRecursive(currentItem.children);

        if (found) {
          return true; // 如果在子项中找到目标项，停止继续查找
        }
      }
    }

    return false; // 表示未找到目标项
  };

  // 从顶层开始递归删除
  deleteRecursive(dataSource);
};

//找出数据中的全部code
export const flattenCodes = (data) => {
  const codes: string[] = [];
  const traverse = (node: { code: string; children: any[] }) => {
    codes.push(node.code);
    if (node.children && node.children.length > 0) {
      node.children.forEach(traverse);
    }
  };
  data.forEach(traverse);
  return codes;
};

// export enum ENABLED {
//   FALSE = 1,
//   TRUE = 0,
// }
