import { useCallback } from 'react';

function clamp(value: number, min: number, max: number): number {
  return Math.min(max, Math.max(min, value));
}

type Axis = 'horizontal' | 'vertical';

export function usePanelResize(
  axis: Axis,
  onResize: (next: number) => void,
  getCurrent: () => number,
  min: number,
  max: number,
  onCommit?: (value: number) => void,
) {
  return useCallback((e: React.PointerEvent<HTMLDivElement>) => {
    e.preventDefault();
    const startX = e.clientX;
    const startY = e.clientY;
    const start = getCurrent();
    const cursor = axis === 'vertical' ? 'col-resize' : 'row-resize';

    document.body.style.cursor = cursor;
    document.body.style.userSelect = 'none';
    e.currentTarget.setPointerCapture(e.pointerId);
    e.currentTarget.classList.add('se-splitter--active');
    const splitter = e.currentTarget;

    const onMove = (ev: PointerEvent) => {
      const delta = axis === 'vertical' ? ev.clientX - startX : startY - ev.clientY;
      onResize(clamp(start + delta, min, max));
    };

    const onUp = (ev: PointerEvent) => {
      document.body.style.cursor = '';
      document.body.style.userSelect = '';
      splitter.releasePointerCapture(ev.pointerId);
      splitter.classList.remove('se-splitter--active');
      window.removeEventListener('pointermove', onMove);
      window.removeEventListener('pointerup', onUp);
      onCommit?.(getCurrent());
    };

    window.addEventListener('pointermove', onMove);
    window.addEventListener('pointerup', onUp);
  }, [axis, onResize, getCurrent, min, max, onCommit]);
}

export function readStoredSize(key: string, fallback: number): number {
  try {
    const raw = localStorage.getItem(key);
    if (!raw) return fallback;
    const n = Number(raw);
    return Number.isFinite(n) ? n : fallback;
  } catch {
    return fallback;
  }
}

export function storeSize(key: string, value: number): void {
  try {
    localStorage.setItem(key, String(Math.round(value)));
  } catch { /* ignore */ }
}
