import { insertJsxAttributes } from "../db";
import type { CodemapDatabase } from "../db";
import type { ParsedJsxAttribute, ParsedJsxElement } from "../extractors/jsx";

export function persistJsxElementsAndAttributes(
  db: CodemapDatabase,
  elements: ParsedJsxElement[],
  attributes: ParsedJsxAttribute[],
): void {
  if (!elements.length) return;
  const idMap = new Map<number, number>();
  for (const el of elements) {
    const row = db
      .query<{ id: number }>(
        `INSERT INTO jsx_elements (
        file_path, component_name, line_start, line_end, column_start, column_end,
        is_self_closing, is_fragment, namespace_prefix, parent_element_id,
        children_count, is_lowercase
      ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) RETURNING id`,
      )
      .get(
        el.file_path,
        el.component_name,
        el.line_start,
        el.line_end,
        el.column_start,
        el.column_end,
        el.is_self_closing,
        el.is_fragment,
        el.namespace_prefix,
        null,
        el.children_count,
        el.is_lowercase,
      );
    idMap.set(el._local_id, row!.id);
  }
  for (const el of elements) {
    if (el._parent_local_id == null) continue;
    const id = idMap.get(el._local_id);
    const parentId = idMap.get(el._parent_local_id);
    if (id != null && parentId != null) {
      db.run("UPDATE jsx_elements SET parent_element_id = ? WHERE id = ?", [
        parentId,
        id,
      ]);
    }
  }
  const attrRows = [];
  for (const attr of attributes) {
    const elementId = idMap.get(attr.element_local_id);
    if (elementId == null) continue;
    attrRows.push({
      element_id: elementId,
      name: attr.name,
      line: attr.line,
      column_start: attr.column_start,
      column_end: attr.column_end,
      value_kind: attr.value_kind,
      value_text: attr.value_text,
    });
  }
  if (attrRows.length) insertJsxAttributes(db, attrRows);
}
