"use client";

import type { KernelFileTreeProjectionItem } from "@/lib/kernel-file-tree";
import {
  getDocIdFromFileTreeItem,
  resolveFileTreeRowAsset,
  resolveFileTreeRowNode,
} from "@/lib/kernel-file-tree";
import type { SidebarTreeNode } from "@/lib/kernel-sidebar";
import type { PageTreeProjectionItem } from "@/lib/tree-projection";
import type { MediaAsset } from "@/types/media";
import type { FileTreeRow } from "./types";
import { makeAssetFolderRowId, makeAssetRowId, makeDocRowId, makeIndexRowId } from "./types";

function buildRowsFromKernelFileTreeProjection(input: {
  fileTreeItems: KernelFileTreeProjectionItem[];
  expanded: Set<string>;
  expandedAssetFolderIds?: Set<string>;
  nodeById?: Map<string, SidebarTreeNode>;
  assetById?: Map<string, MediaAsset>;
}): FileTreeRow[] {
  const rows: FileTreeRow[] = [];
  const seen = new Set<string>();

  input.fileTreeItems.forEach((item) => {
    if (!item?.rowId || seen.has(item.rowId)) {
      return;
    }
    seen.add(item.rowId);
    const docId = getDocIdFromFileTreeItem(item);

    switch (item.rowKind) {
      case "document": {
        const node = resolveFileTreeRowNode(item, input.nodeById);
        rows.push({
          kind: "doc",
          rowId: makeDocRowId(docId),
          depth: item.depth,
          docId,
          parentDocId: item.parentNodeId,
          node,
          hasChildren: item.childCount > 0,
          isExpanded: input.expanded.has(docId),
        });
        return;
      }
      case "index": {
        const node = resolveFileTreeRowNode(item, input.nodeById);
        rows.push({
          kind: "index",
          rowId: makeIndexRowId(docId),
          depth: item.depth,
          docId,
          parentDocId: docId,
          node,
        });
        return;
      }
      case "asset_folder": {
        const asset = resolveFileTreeRowAsset(item, input.assetById);
        rows.push({
          kind: "asset-folder",
          rowId: makeAssetFolderRowId(asset.id),
          depth: item.depth,
          docId,
          parentDocId: docId,
          asset,
          hasChildren: item.childCount > 0,
          isExpanded: input.expandedAssetFolderIds?.has(asset.id) ?? false,
        });
        return;
      }
      case "asset": {
        const asset = resolveFileTreeRowAsset(item, input.assetById);
        rows.push({
          kind: "asset",
          rowId: makeAssetRowId(asset.id),
          depth: item.depth,
          docId,
          parentDocId: docId,
          asset,
        });
        return;
      }
    }
  });

  return rows;
}

export function buildVisibleRows({
  fileTreeItems,
  expanded,
  expandedAssetFolderIds,
  nodeById,
  assetById,
}: {
  fileTreeItems?: KernelFileTreeProjectionItem[];
  // 兼容旧调用签名；主路径必须提供 kernel file_tree items，不能再从这些字段重建对象语义。
  pageRows?: PageTreeProjectionItem[];
  expanded: Set<string>;
  assetsByDoc?: Record<string, MediaAsset[]>;
  assetChildrenByAssetId?: Record<string, MediaAsset[]>;
  expandedAssetFolderIds?: Set<string>;
  nodeById?: Map<string, SidebarTreeNode>;
  assetById?: Map<string, MediaAsset>;
}): FileTreeRow[] {
  if (fileTreeItems && fileTreeItems.length > 0) {
    return buildRowsFromKernelFileTreeProjection({
      fileTreeItems,
      expanded,
      expandedAssetFolderIds,
      nodeById,
      assetById,
    });
  }

  return [];
}
