'use client';

import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuSeparator,
  ContextMenuShortcut,
  ContextMenuTrigger,
} from '@djangocfg/ui-core/components';

import type {
  TreeContextMenuItem,
  TreeRowRenderProps,
} from '../../types';

/**
 * Render an array of declarative menu items as a themed `<ContextMenu>`
 * wrapped around the supplied trigger element. Pure presentational layer
 * — the caller resolves and merges items.
 */
export function renderItemsAsContextMenu<T>(
  rowProps: TreeRowRenderProps<T>,
  items: TreeContextMenuItem<T>[],
  trigger: React.ReactNode,
): React.ReactNode {
  return (
    <ContextMenu>
      <ContextMenuTrigger asChild>{trigger}</ContextMenuTrigger>
      <ContextMenuContent>
        {items.map((item, idx) => {
          if (item === 'separator') {
            return <ContextMenuSeparator key={`sep-${idx}`} />;
          }
          const Icon = item.icon;
          return (
            <ContextMenuItem
              key={item.id}
              disabled={item.disabled}
              variant={item.destructive ? 'destructive' : undefined}
              onSelect={() => item.onSelect(rowProps)}
            >
              {Icon ? <Icon /> : null}
              {item.label}
              {item.shortcut ? (
                <ContextMenuShortcut>{item.shortcut}</ContextMenuShortcut>
              ) : null}
            </ContextMenuItem>
          );
        })}
      </ContextMenuContent>
    </ContextMenu>
  );
}

/**
 * Drop trailing / leading / duplicate separators so a merged menu never
 * shows a separator next to a section header or another separator.
 */
export function tidyMenuItems<T>(
  items: TreeContextMenuItem<T>[],
): TreeContextMenuItem<T>[] {
  const out: TreeContextMenuItem<T>[] = [];
  for (const it of items) {
    if (it === 'separator') {
      if (out.length === 0) continue;
      if (out[out.length - 1] === 'separator') continue;
      out.push(it);
    } else {
      out.push(it);
    }
  }
  while (out.length > 0 && out[out.length - 1] === 'separator') out.pop();
  return out;
}
