import {
  DATA_SPACES,
  DIRECTORY_TREE,
  FILES_BY_FOLDER,
  DEFAULT_FOLDER_ID
} from '@/mock/dataSpace.js'

/**
 * 获取数据空间列表
 * @returns {Promise<{success: boolean, array: Array}>}
 */
export function getDataSpaces() {
  return Promise.resolve({ success: true, array: DATA_SPACES })
}

/**
 * 获取目录树
 * @returns {Promise<{success: boolean, array: Array}>}
 */
export function getDirectoryTree() {
  return Promise.resolve({ success: true, array: DIRECTORY_TREE })
}

/**
 * 获取目录下文件列表
 * @param {Object} params
 * @returns {Promise<{success: boolean, array: Array, folder: Object}>}
 */
export function getFolderFiles(params = {}) {
  const { folder_id = DEFAULT_FOLDER_ID } = params
  const files = FILES_BY_FOLDER[folder_id] || []
  return Promise.resolve({
    success: true,
    folder_id,
    array: [...files]
  })
}

/**
 * 根据节点 ID 查找树节点
 * @param {string} nodeId
 * @param {Array} nodes
 * @returns {Object|null}
 */
export function findTreeNode(nodeId, nodes = DIRECTORY_TREE) {
  for (const node of nodes) {
    if (node.id === nodeId) return node
    if (node.children?.length) {
      const found = findTreeNode(nodeId, node.children)
      if (found) return found
    }
  }
  return null
}
