/* Brimoza — themed confirm/alert modal. Replaces native confirm()/alert() so the
   text is a DOM node (translated by i18n) and the dialog is styled (not the bare
   "<host> says" browser box). API: await uiConfirm(msg) -> bool; uiAlert(msg). */
(function () {
  "use strict";
  function build() {
    if (document.getElementById("bz-modal")) return;
    var w = document.createElement("div");
    w.id = "bz-modal";
    w.setAttribute("style", "position:fixed;inset:0;z-index:10000;display:none;align-items:center;justify-content:center;background:rgba(3,6,10,.66)");
    w.innerHTML =
      '<div role="dialog" aria-modal="true" style="background:#161b22;border:1px solid #2a313c;border-radius:14px;max-width:460px;width:90%;padding:22px 24px;box-shadow:0 24px 70px rgba(0,0,0,.6)">'
      + '<div id="bz-modal-msg" style="color:#e6edf3;font-size:14px;line-height:1.6;white-space:pre-line"></div>'
      + '<div style="display:flex;gap:10px;justify-content:flex-end;margin-top:22px">'
      + '<button id="bz-modal-cancel" type="button" style="padding:9px 18px;border-radius:9px;border:1px solid #2a313c;background:#20262f;color:#e6edf3;font-size:13px;font-weight:600;cursor:pointer">İptal</button>'
      + '<button id="bz-modal-ok" type="button" style="padding:9px 18px;border-radius:9px;border:0;background:#1ecb8b;color:#06251a;font-size:13px;font-weight:700;cursor:pointer">Onayla</button>'
      + "</div></div>";
    document.body.appendChild(w);
  }
  function show(msg, withCancel) {
    build();
    return new Promise(function (resolve) {
      var w = document.getElementById("bz-modal"),
          m = document.getElementById("bz-modal-msg"),
          ok = document.getElementById("bz-modal-ok"),
          cc = document.getElementById("bz-modal-cancel");
      m.textContent = msg;                 // text node -> i18n translates it
      cc.style.display = withCancel ? "" : "none";
      w.style.display = "flex";
      function close(v) { w.style.display = "none"; ok.onclick = cc.onclick = w.onclick = document.onkeydown = null; resolve(v); }
      ok.onclick = function () { close(true); };
      cc.onclick = function () { close(false); };
      w.onclick = function (e) { if (e.target === w && withCancel) close(false); };
      document.onkeydown = function (e) { if (e.key === "Escape" && withCancel) close(false); else if (e.key === "Enter") close(true); };
      try { ok.focus(); } catch (e) {}
    });
  }
  window.uiConfirm = function (msg) { return show(msg, true); };
  window.uiAlert = function (msg) { return show(msg, false); };
})();
