(function () {
  "use strict";

  var pendingErrors = [];
  var pendingCheckpoints = [];
  var MAX_PENDING = 20;

  function api() {
    return window.pywebview && window.pywebview.api ? window.pywebview.api : null;
  }

  function safeText(value, limit) {
    var text = "";
    try {
      text = value == null ? "" : String(value);
    } catch (_error) {
      text = "[unavailable]";
    }
    return text.length > limit ? text.slice(0, limit) + "... [truncated]" : text;
  }

  function workflowContext() {
    var value = document.body && document.body.dataset ? document.body.dataset.workflowScope : "";
    return safeText(value || "startup", 40);
  }

  function sendError(payload) {
    var bridge = api();
    if (!bridge || typeof bridge.report_frontend_error !== "function") {
      pendingErrors.push(payload);
      pendingErrors = pendingErrors.slice(-MAX_PENDING);
      return;
    }
    try {
      Promise.resolve(bridge.report_frontend_error(payload)).catch(function () {});
    } catch (_error) {}
  }

  function checkpoint(stage, details) {
    var item = { stage: safeText(stage, 200), details: safeText(details, 2000) };
    var bridge = api();
    if (!bridge || typeof bridge.report_frontend_status !== "function") {
      pendingCheckpoints.push(item);
      pendingCheckpoints = pendingCheckpoints.slice(-MAX_PENDING);
      return;
    }
    try {
      Promise.resolve(bridge.report_frontend_status(item.stage, item.details)).catch(function () {});
    } catch (_error) {}
  }

  function flush() {
    var errors = pendingErrors.slice();
    var checkpoints = pendingCheckpoints.slice();
    pendingErrors = [];
    pendingCheckpoints = [];
    checkpoints.forEach(function (item) { checkpoint(item.stage, item.details); });
    errors.forEach(function (item) { sendError(item); });
  }

  function reportCaught(error, context) {
    var value = error || {};
    sendError({
      kind: "caught",
      message: safeText(value.message || value, 4000),
      stack: safeText(value.stack, 12000),
      stage: safeText(context && context.stage, 200),
      operation: safeText(context && context.operation, 200),
      workflow: workflowContext()
    });
  }

  window.addEventListener("error", function (event) {
    sendError({
      kind: "window.error",
      message: safeText(event.message || (event.error && event.error.message) || "Unknown JavaScript error", 4000),
      source: safeText(event.filename, 2000),
      line: event.lineno || 0,
      column: event.colno || 0,
      stack: safeText(event.error && event.error.stack, 12000),
      workflow: workflowContext()
    });
  });

  window.addEventListener("unhandledrejection", function (event) {
    var reason = event.reason || {};
    sendError({
      kind: "unhandledrejection",
      message: safeText(reason.message || reason, 4000),
      stack: safeText(reason.stack, 12000),
      workflow: workflowContext()
    });
  });

  window.addEventListener("pywebviewready", function () {
    flush();
    checkpoint("bridge_ready", "pywebviewready received");
  }, { once: true });

  window.LiveryLabStartupLog = {
    checkpoint: checkpoint,
    reportError: reportCaught,
    flush: flush
  };
  checkpoint("script_initialized", "startup_logging.js loaded");
})();
