const warned = new Set();

export const warnOnce = (code, message, details = {}) => {
  const key = `${code}:${message}`;
  if (warned.has(key)) {return;}
  warned.add(key);
  console.warn(`[${code}] ${message}`, details);
};

export const requireEl = (selector, details = {}) => {
  const el = document.querySelector(selector);
  if (!el) {
    warnOnce('UI_EL_MISSING', `Missing element: ${selector}`, details);
    return null;
  }
  return el;
};

export const requireAll = (selector, details = {}) => {
  const list = Array.from(document.querySelectorAll(selector));
  if (!list.length) {
    warnOnce('UI_EL_MISSING', `Missing elements: ${selector}`, details);
  }
  return list;
};

export const optionalEl = (selector) => document.querySelector(selector);
