/* global React */
const { useState, useMemo, useEffect, useRef } = React;
const D = window.LAGER_DATA;

// ---------- Icons (minimal stroke icons, no SVG illustrations) ----------
const Ico = {
  dash: (p)   => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><rect x="2" y="2" width="5" height="5" rx="1"/><rect x="9" y="2" width="5" height="3" rx="1"/><rect x="2" y="9" width="5" height="5" rx="1"/><rect x="9" y="7" width="5" height="7" rx="1"/></svg>,
  bar: (p)    => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M3 6h10l-1 7H4z"/><path d="M5 6V3h6v3"/></svg>,
  count: (p)  => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><rect x="2.5" y="2.5" width="11" height="11" rx="2"/><path d="M5 6h6M5 9h6M5 12h4"/></svg>,
  order: (p)  => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M2 3h2l1.5 8h7.5L14 5H4.5"/><circle cx="6" cy="13.5" r="1"/><circle cx="12" cy="13.5" r="1"/></svg>,
  retur: (p)  => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M3 5l3-3 3 3"/><path d="M6 2v8a3 3 0 003 3h4"/></svg>,
  doc: (p)    => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M3 1.5h6l4 4V14a.5.5 0 01-.5.5h-9A.5.5 0 013 14z"/><path d="M9 1.5V6h4"/></svg>,
  warn: (p)   => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M8 2l6.5 11h-13z"/><path d="M8 7v3M8 12v.5"/></svg>,
  set: (p)    => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><circle cx="8" cy="8" r="2"/><path d="M8 1v2M8 13v2M15 8h-2M3 8H1M12.95 3.05l-1.4 1.4M4.45 11.55l-1.4 1.4M12.95 12.95l-1.4-1.4M4.45 4.45L3.05 3.05"/></svg>,
  search: (p) => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><circle cx="7" cy="7" r="4.5"/><path d="M10.5 10.5L14 14"/></svg>,
  plus: (p)   => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M8 3v10M3 8h10"/></svg>,
  up: (p)     => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M8 12V4M4 8l4-4 4 4"/></svg>,
  upload: (p) => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M8 10V2M5 5l3-3 3 3M2 12v1.5a.5.5 0 00.5.5h11a.5.5 0 00.5-.5V12"/></svg>,
  filter: (p) => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M2 3h12l-4.5 6V14L6.5 12V9z"/></svg>,
  check: (p)  => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M3 8.5L6.5 12 13 4.5"/></svg>,
  arrow: (p)  => <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}><path d="M4 8h8M8.5 4.5L13 8l-4.5 3.5"/></svg>,
};

// ---------- Helpers ----------
const product = (pid) => D.products.find(p => p.id === pid);
const bar = (bid) => D.bars.find(b => b.id === bid);
const fmtKr = (n) => new Intl.NumberFormat("da-DK").format(n) + " kr";
const stockStatus = (row) => {
  if (row.current === 0) return "danger";
  if (row.current < row.par) return "warn";
  return "ok";
};
const barRollup = (bid) => {
  const rows = D.stock[bid] || [];
  const total = rows.length;
  const low = rows.filter(r => r.current < r.par).length;
  const empty = rows.filter(r => r.current === 0).length;
  const value = rows.reduce((s, r) => s + r.current * (product(r.pid)?.price || 0), 0);
  return { total, low, empty, value };
};

// ---------- Pill ----------
const Pill = ({ tone, children, dot }) => (
  <span className={"pill " + (tone || "")}>{dot && <span className="dot"/>}{children}</span>
);

// ---------- Stepper input ----------
const Stepper = ({ value, onChange, min = 0, step = 1 }) => (
  <div className="stepper">
    <button type="button" onClick={() => onChange(Math.max(min, value - step))}>−</button>
    <input type="text" inputMode="numeric" value={value} onChange={(e) => {
      const v = parseInt(e.target.value || "0", 10);
      onChange(isNaN(v) ? 0 : Math.max(min, v));
    }}/>
    <button type="button" onClick={() => onChange(value + step)}>+</button>
  </div>
);

// ---------- Level bar with par marker ----------
const Level = ({ current, max, par }) => {
  const pct = Math.min(100, Math.max(0, (current / max) * 100));
  const parPct = Math.min(100, Math.max(0, (par / max) * 100));
  const status = current === 0 ? "danger" : current < par ? "warn" : "ok";
  return (
    <div className="level" title={`${current} / ${max} (min ${par})`}>
      <div className={"level-fill " + status} style={{ width: pct + "%" }}/>
      <div className="par-mark" style={{ left: parPct + "%" }}/>
    </div>
  );
};

const DAY_NAMES_DA = ["Søndag","Mandag","Tirsdag","Onsdag","Torsdag","Fredag","Lørdag"];
const festivalDay = () => {
  const start = new Date(D.festival.startDate + "T00:00:00");
  const end   = new Date(D.festival.endDate   + "T00:00:00");
  const today = new Date(); today.setHours(0, 0, 0, 0);
  const total = Math.round((end - start) / 86400000) + 1;
  const num   = Math.min(Math.max(Math.round((today - start) / 86400000) + 1, 1), total);
  const date  = new Date(start.getTime() + (num - 1) * 86400000);
  return { num, total, name: DAY_NAMES_DA[date.getDay()] };
};

window.LagerUI = { Ico, Pill, Stepper, Level, product, bar, fmtKr, stockStatus, barRollup, festivalDay };
