// Tiny hyperscript helper for vanilla DOM building.
// Resolve automaticamente src/href que apontem para um dos publicRoots do brand
// (setados via createApp), respeitando o BASE_URL do Vite (GitLab Pages base path).
import { publicAsset } from './utils/publicAsset.js';
import { getPublicRoots } from './config.js';

function attrValue(k, v) {
  if ((k === 'src' || k === 'href') && typeof v === 'string') {
    const clean = v.replace(/^\/+/, '');
    if (getPublicRoots().some(root => clean.startsWith(root))) return publicAsset(clean);
  }

  return v;
}

export function h(tag, attrs = {}, ...children) {
  const el = document.createElement(tag);
  for (const [k, v] of Object.entries(attrs || {})) {
    if (k === 'class') el.className = v;
    else if (k === 'html') el.innerHTML = v;
    else if (k === 'style' && typeof v === 'object') Object.assign(el.style, v);
    else if (k.startsWith('on') && typeof v === 'function') el.addEventListener(k.slice(2).toLowerCase(), v);
    else if (v !== false && v != null) el.setAttribute(k, attrValue(k, v));
  }
  for (const c of children.flat()) {
    if (c == null || c === false) continue;
    el.append(c instanceof Node ? c : document.createTextNode(String(c)));
  }
  return el;
}
