/* Zdieľané UI prvky */

function PositionPill({ pos }) {
  const p = TD.POSITIONS[pos];
  return <span className="pos"><span className="pdot" style={{ background: p.color }}></span>{p.label}</span>;
}

function Avatar({ emp, size }) {
  const s = size || 44;
  return <div className="avatar" style={{ background: emp.av, width: s, height: s, fontSize: s * 0.36 }}>{emp.initials}</div>;
}

function StatusBadge({ status }) {
  if (TD.ABSENCE[status]) {
    const a = TD.ABSENCE[status];
    return <span className="badge abs" style={{ color: a.color, background: a.color + "1A", borderColor: a.color + "44" }}><span className="dot" style={{ background: a.color }}></span>{a.label.split(" — ")[0]}</span>;
  }
  const map = {
    done:   ["ok",   "Odpracované"],
    done_late:["late","Meškanie"],
    working:["work", "V práci"],
    working_late:["work","V práci"],
    planned:["off",  "Plánované"],
  };
  const [cls, label] = map[status] || ["off", status];
  return <span className={"badge " + cls}><span className="dot"></span>{label}</span>;
}

/* malý kódový čip neprítomnosti (D / PN / OČR / P / NV) */
function AbsChip({ type, size }) {
  const a = TD.ABSENCE[type];
  if (!a) return null;
  return <span className="abs-chip" title={a.label} style={{ color: a.color, background: a.color + "1F", borderColor: a.color + "55", fontSize: size || 11 }}>{a.code}</span>;
}

function ShiftTag({ shift }) {
  const s = TD.SHIFTS[shift];
  if (!s) return null;
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 7, fontSize: 13 }}>
      <span style={{ width: 8, height: 8, borderRadius: 3, background: s.color }}></span>
      <b style={{ fontWeight: 600 }}>{s.label}</b>
      <span className="faint tnum">{s.start}–{s.end}</span>
    </span>
  );
}

function fmtH(h) { return (Math.round(h * 100) / 100).toFixed(2).replace(".", ",") + " h"; }
function fmtEur(n) { return n.toFixed(2).replace(".", ",") + " €"; }
function fmtDateLong(d) {
  const dt = typeof d === "string" ? new Date(d) : d;
  return TD.DOW[(dt.getDay() + 6) % 7] + " " + dt.getDate() + ". " + TD.MONTHS[dt.getMonth()] + " " + dt.getFullYear();
}

Object.assign(window, { PositionPill, Avatar, StatusBadge, AbsChip, ShiftTag, fmtH, fmtEur, fmtDateLong });
