/**
 * Tauri adapter for {@link LayoutGateway} (L4). Together with the sibling
 * adapters this is the *only* place that calls `invoke()`; components reach it
 * exclusively through the port.
 *
 * Commands and payload keys are camelCase, matching the backend DTO convention.
 * The `load_layout`/`mutate_layout` commands return the layout tree directly
 * (the backend `LayoutDto` is `#[serde(transparent)]` over the tree).
 */

import { invoke } from "@tauri-apps/api/core";

import type { LayoutKind, LayoutList, LayoutOperation, LayoutTree } from "@/domain";
import type { LayoutGateway } from "@/ports";

export class TauriLayoutGateway implements LayoutGateway {
  loadLayout(projectId: string, layoutId?: string): Promise<LayoutTree> {
    return invoke<LayoutTree>("load_layout", { projectId, layoutId });
  }

  mutateLayout(
    projectId: string,
    operation: LayoutOperation,
    layoutId?: string,
  ): Promise<LayoutTree> {
    return invoke<LayoutTree>("mutate_layout", { projectId, layoutId, operation });
  }

  listLayouts(projectId: string): Promise<LayoutList> {
    return invoke<LayoutList>("list_layouts", { projectId });
  }

  createLayout(projectId: string, name: string, kind?: LayoutKind): Promise<{ layoutId: string }> {
    return invoke<{ layoutId: string }>("create_layout", {
      request: { projectId, name, kind },
    });
  }

  renameLayout(projectId: string, layoutId: string, name: string): Promise<void> {
    return invoke<void>("rename_layout", {
      request: { projectId, layoutId, name },
    });
  }

  deleteLayout(projectId: string, layoutId: string): Promise<{ activeId: string }> {
    return invoke<{ activeId: string }>("delete_layout", {
      request: { projectId, layoutId },
    });
  }

  setActiveLayout(projectId: string, layoutId: string): Promise<{ activeId: string }> {
    return invoke<{ activeId: string }>("set_active_layout", {
      request: { projectId, layoutId },
    });
  }
}
