'use client';

// Resizable Panels — NiceUI (MIT) · https://niceui.dev/c/components/resizable-panels
// One self-contained file: needs only React and Tailwind CSS. Nothing else to install.

import { useRef, useState, type KeyboardEvent as ReactKeyboardEvent, type PointerEvent as RPointerEvent, type ReactNode, createElement, type SVGProps } from 'react';

function cn(...classes: Array<string | false | null | undefined>) {
  return classes.filter(Boolean).join(' ');
}

// Icons from Lucide (ISC licence, lucide.dev), inlined so there is nothing to install.
type NiceIconProps = SVGProps<SVGSVGElement> & { size?: number | string; absoluteStrokeWidth?: boolean };
type LucideIcon = (props: NiceIconProps) => JSX.Element;
type LucideProps = NiceIconProps;
const niceIcon = (node: Array<[string, Record<string, string>]>): LucideIcon =>
  function Icon({ size = 24, strokeWidth = 2, absoluteStrokeWidth, children, ...rest }: NiceIconProps) {
    const sw = absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth;
    return createElement('svg', { xmlns: 'http://www.w3.org/2000/svg', width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: sw, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true, ...rest }, ...node.map(([tag, attrs], i) => createElement(tag, { key: i, ...attrs })), children);
  };
const FileCode2 = niceIcon([["path",{"d":"M4 22h14a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v4"}],["path",{"d":"M14 2v4a2 2 0 0 0 2 2h4"}],["path",{"d":"m5 12-3 3 3 3"}],["path",{"d":"m9 18 3-3-3-3"}]]);
const Folder = niceIcon([["path",{"d":"M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"}]]);
const TerminalSquare = niceIcon([["path",{"d":"m7 11 2-2-2-2"}],["path",{"d":"M11 13h4"}],["rect",{"width":"18","height":"18","x":"3","y":"3","rx":"2","ry":"2"}]]);

// ── components/controls/ResizablePanels.tsx ─────────────────────
export type ResizablePanelsProps = {
  /** Two panels. */
  children: [ReactNode, ReactNode];
  direction?: 'horizontal' | 'vertical';
  /** Size of the first panel in percent. */
  defaultSize?: number;
  /** Minimum size of each panel in percent. */
  min?: number;
  label?: string;
  className?: string;
  onResize?: (size: number) => void;
};

/**
 * Two resizable panels with a drag handle. The handle is a focusable separator:
 * arrow keys resize (Shift for bigger steps), Home/End collapse to the limits,
 * and double-click or Enter resets.
 */
export function ResizablePanels({ children, direction = 'horizontal', defaultSize = 50, min = 15, label = 'Resize panels', className, onResize }: ResizablePanelsProps) {
  const [size, setSize] = useState(defaultSize);
  const [drag, setDrag] = useState(false);
  const box = useRef<HTMLDivElement>(null);
  const h = direction === 'horizontal';
  const clamp = (v: number) => Math.min(100 - min, Math.max(min, v));
  const set = (v: number) => {
    const c = clamp(v);
    setSize(c);
    onResize?.(c);
  };

  const fromPointer = (e: RPointerEvent) => {
    const r = box.current!.getBoundingClientRect();
    set(h ? ((e.clientX - r.left) / r.width) * 100 : ((e.clientY - r.top) / r.height) * 100);
  };
  const onKey = (e: ReactKeyboardEvent) => {
    const step = e.shiftKey ? 10 : 2;
    const back = h ? 'ArrowLeft' : 'ArrowUp';
    const fwd = h ? 'ArrowRight' : 'ArrowDown';
    if (e.key === back) set(size - step);
    else if (e.key === fwd) set(size + step);
    else if (e.key === 'Home') set(min);
    else if (e.key === 'End') set(100 - min);
    else if (e.key === 'Enter') set(defaultSize);
    else return;
    e.preventDefault();
  };

  return (
    <div ref={box} className={cn('flex h-full w-full overflow-hidden', h ? 'flex-row' : 'flex-col', drag && 'select-none', className)}>
      <div className="min-h-0 min-w-0 overflow-auto" style={{ flexBasis: `${size}%`, flexShrink: 0 }}>
        {children[0]}
      </div>
      <div
        role="separator"
        aria-orientation={h ? 'vertical' : 'horizontal'}
        aria-label={label}
        aria-valuenow={Math.round(size)}
        aria-valuemin={min}
        aria-valuemax={100 - min}
        tabIndex={0}
        onPointerDown={(e) => {
          e.currentTarget.setPointerCapture(e.pointerId);
          setDrag(true);
        }}
        onPointerMove={(e) => drag && fromPointer(e)}
        onPointerUp={() => setDrag(false)}
        onPointerCancel={() => setDrag(false)}
        onDoubleClick={() => set(defaultSize)}
        onKeyDown={onKey}
        className={cn(
          'group relative z-10 flex shrink-0 touch-none items-center justify-center outline-none',
          h ? 'w-px cursor-col-resize bg-white/[.08]' : 'h-px cursor-row-resize bg-white/[.08]',
        )}
      >
        {/* wide invisible hit area */}
        <span className={cn('absolute', h ? 'inset-y-0 -left-2 -right-2' : 'inset-x-0 -top-2 -bottom-2')} />
        <span
          className={cn(
            'pointer-events-none absolute rounded-full transition-all duration-200',
            h ? 'h-8 w-1' : 'h-1 w-8',
            drag ? 'scale-110 bg-[#38bdf8]' : 'bg-white/20 group-hover:bg-white/50 group-focus-visible:bg-[#38bdf8]',
          )}
        />
        <span className={cn('pointer-events-none absolute transition-colors', h ? 'inset-y-0 w-px' : 'inset-x-0 h-px', drag ? 'bg-[#38bdf8]/70' : 'group-hover:bg-white/25 group-focus-visible:bg-[#38bdf8]/60')} />
      </div>
      <div className="min-h-0 min-w-0 flex-1 overflow-auto">{children[1]}</div>
    </div>
  );
}

const FILES = ['app.tsx', 'layout.tsx', 'page.tsx', 'globals.css', 'utils.ts'];
const CODE = [
  ['import', ' { ResizablePanels } ', 'from', " '@/components/controls/ResizablePanels';"],
  ['', '', '', ''],
  ['export function', ' Editor() {', '', ''],
  ['  return', ' (', '', ''],
  ['    <ResizablePanels', ' direction="vertical"', '>', ''],
  ['      <Code />', '', '', ''],
  ['      <Terminal />', '', '', ''],
  ['    </ResizablePanels>', '', '', ''],
  ['  );', '', '', ''],
  ['}', '', '', ''],
];

export function ResizablePanelsDemo() {
  const [file, setFile] = useState(0);
  return (
    <div className="h-[380px] w-full max-w-3xl overflow-hidden rounded-2xl border border-white/[.08] bg-[#000000] shadow-[0_0_0_1px_rgba(255,255,255,0.06),0_1px_2px_rgba(0,0,0,0.4),0_8px_24px_-8px_rgba(0,0,0,0.5)]">
      <ResizablePanels defaultSize={28} min={18} label="Resize sidebar">
        <div className="h-full bg-[#0b0c0e]/60 p-2">
          <p className="flex items-center gap-1.5 px-2 py-1.5 text-[11px] uppercase tracking-wider text-[#6b7079]">
            <Folder className="h-3.5 w-3.5" /> src
          </p>
          {FILES.map((f, i) => (
            <button key={f} onClick={() => setFile(i)} className={cn('flex w-full items-center gap-2 truncate rounded-md px-2 py-1.5 text-left text-[12.5px]', i === file ? 'bg-[#38bdf8]/10 text-[#bae6fd]' : 'text-[#a3a8b0] hover:bg-white/[.04]')}>
              <FileCode2 className="h-3.5 w-3.5 shrink-0 opacity-70" /> <span className="truncate">{f}</span>
            </button>
          ))}
        </div>
        <ResizablePanels direction="vertical" defaultSize={64} min={20} label="Resize terminal">
          <div className="h-full p-4 font-mono text-[12px] leading-6">
            <p className="mb-2 text-[11px] text-[#6b7079]">{FILES[file]}</p>
            {CODE.map((l, i) => (
              <p key={i} className="whitespace-pre">
                <span className="mr-4 inline-block w-4 text-right text-[#4b5058]">{i + 1}</span>
                <span className="text-fuchsia-300">{l[0]}</span>
                <span className="text-[#e5e7eb]">{l[1]}</span>
                <span className="text-fuchsia-300">{l[2]}</span>
                <span className="text-emerald-300">{l[3]}</span>
              </p>
            ))}
          </div>
          <div className="h-full bg-black/40 p-3 font-mono text-[12px] leading-6 text-[#a3a8b0]">
            <p className="mb-1 flex items-center gap-1.5 text-[11px] text-[#6b7079]">
              <TerminalSquare className="h-3.5 w-3.5" /> Terminal
            </p>
            <p>
              <span className="text-[#7dd3fc]">~/app</span> $ npm run dev
            </p>
            <p className="text-emerald-300">✓ Ready in 412ms</p>
            <p className="text-[#6b7079]">➜ Local: http://localhost:5173</p>
          </div>
        </ResizablePanels>
      </ResizablePanels>
    </div>
  );
}
