/* ════════════════════════════════════════════════════════════════
   rsToast.js · tiny dependency-free toast (non-blocking, auto-dismiss)
   ----------------------------------------------------------------------------
   Include once (after your other scripts), then call anywhere:

       rsToast("Saved");                       // success (default)
       rsToast("Project manager assigned", "success");
       rsToast("Couldn't reach the server", "error", 4000);
       rsToast("Heads up — check the dates", "warning");
       rsToast("No price schedule.", "error", 6000, {
           url: "/ServiceAsset/equipment/42", label: "Add price schedule"
       });                                     // optional action link

   Behaviour: auto-dismisses after `ms` (default 2800). Hovering the toast
   pauses the timer; leaving resumes it. Clicking or clicking the link
   dismisses immediately.

   Inside a Vue app you can wrap it as a method if you like:
       _toast(msg, type) { rsToast(msg, type); }

   Non-blocking: it does NOT cover the screen, needs no click, and auto-dismisses
   (click it to dismiss early). Use $.confirm for confirmations and $.alert for
   errors that must be acknowledged.
   ════════════════════════════════════════════════════════════════ */
(function () {
  if (window.rsToast) return; // already loaded

  var STYLE_ID = "rs-toast-style";
  if (!document.getElementById(STYLE_ID)) {
    var css = document.createElement("style");
    css.id = STYLE_ID;
    css.textContent =
      ".rs-toast-wrap{position:fixed;top:16px;right:16px;z-index:99999;display:flex;" +
      "flex-direction:column;gap:8px;max-width:340px;pointer-events:none;}" +
      ".rs-toast{pointer-events:auto;display:flex;align-items:flex-start;gap:10px;" +
      "background:#1f272e;color:#fff;border-radius:6px;padding:11px 14px;font-size:13px;" +
      "line-height:1.35;box-shadow:0 6px 24px rgba(0,0,0,.18);border-left:4px solid #6c7680;" +
      "opacity:0;transform:translateX(20px);transition:opacity .22s ease,transform .22s ease;" +
      "cursor:pointer;}" +
      ".rs-toast.rs-in{opacity:1;transform:translateX(0);}" +
      ".rs-toast.success{border-left-color:#10b981;}" +
      ".rs-toast.error{border-left-color:#dc2626;}" +
      ".rs-toast.warning{border-left-color:#d97706;}" +
      ".rs-toast.info{border-left-color:#3d4a66;}" +
      ".rs-toast .rs-toast-ic{flex:0 0 auto;margin-top:1px;}" +
      ".rs-toast .rs-toast-msg{flex:1 1 auto;}" +
      ".rs-toast .rs-toast-link{display:inline-block;margin-top:6px;color:#93c5fd;" +
      "text-decoration:underline;font-weight:500;}" +
      ".rs-toast .rs-toast-link:hover{color:#fff;}";
    document.head.appendChild(css);
  }

  function wrap() {
    var w = document.querySelector(".rs-toast-wrap");
    if (!w) {
      w = document.createElement("div");
      w.className = "rs-toast-wrap";
      document.body.appendChild(w);
    }
    return w;
  }

  var ICONS = {
    success: "fa fa-circle-check",
    error: "fa fa-circle-exclamation",
    warning: "fa fa-triangle-exclamation",
    info: "fa fa-circle-info",
  };

  window.rsToast = function (message, type, ms, action) {
    type = type || "success";
    ms = typeof ms === "number" ? ms : 2800;

    var el = document.createElement("div");
    el.className = "rs-toast " + type;

    var ic = document.createElement("i");
    ic.className = "rs-toast-ic " + (ICONS[type] || ICONS.info);
    var body = document.createElement("div");
    body.className = "rs-toast-msg";
    var msg = document.createElement("span");
    msg.innerHTML = message == null ? "" : String(message);
    body.appendChild(msg);

    // Optional action link (e.g. "Add price schedule").
    if (action && action.url && action.label) {
      body.appendChild(document.createElement("br"));
      var a = document.createElement("a");
      a.className = "rs-toast-link";
      a.href = action.url;
      a.textContent = action.label;
      body.appendChild(a);
    }

    el.appendChild(ic);
    el.appendChild(body);

    wrap().appendChild(el);
    // force reflow so the transition runs
    void el.offsetWidth;
    el.classList.add("rs-in");

    var timer = null;
    var remove = function () {
      if (!el.parentNode) return;
      el.classList.remove("rs-in");
      setTimeout(function () {
        if (el.parentNode) el.parentNode.removeChild(el);
      }, 240);
    };
    var startTimer = function () {
      if (ms > 0 && !timer) timer = setTimeout(remove, ms);
    };
    var clearTimer = function () {
      if (timer) { clearTimeout(timer); timer = null; }
    };

    // Click the toast body (but not the link) to dismiss.
    el.addEventListener("click", function (ev) {
      if (ev.target && ev.target.tagName === "A") return; // let the link navigate
      clearTimer();
      remove();
    });
    // Hover the toast to keep it around while the user reads/uses it.
    if (ms > 0) {
      el.addEventListener("mouseenter", clearTimer);
      el.addEventListener("mouseleave", startTimer);
    }
    startTimer();
    return el;
  };
})();