import { getSettings, setSettings } from "./lib/storage.js";
import {
  getLicenseState,
  validateLicense,
  clearLicense,
  emptyLicenseState,
} from "./lib/license.js";
const a0_0x1ed1a0 = (() => {
  try {
    if (
      typeof chrome === "undefined" ||
      !chrome.runtime ||
      !chrome.runtime.id
    ) {
      return false;
    }
    const _0x407c45 = chrome.runtime.getManifest?.();
    if (!_0x407c45) {
      return false;
    }
    if (_0x407c45.name !== "Box Lovable") {
      return false;
    }
    if (_0x407c45.manifest_version !== 3) {
      return false;
    }
    return true;
  } catch (_0x32b02a) {
    return false;
  }
})();
chrome.runtime.onMessage.addListener((_0x2a40a6, _0x32547a, _0x442233) => {
  if (!a0_0x1ed1a0) {
    _0x442233({
      ok: false,
      error: "runtime invalid",
    });
    return;
  }
  return a0_0x2f0359(_0x2a40a6, _0x32547a, _0x442233);
});
function a0_0x2f0359(_0x53dfc7, _0xe9f274, _0x36e23e) {
  (async () => {
    try {
      switch (_0x53dfc7.type) {
        case "GET_SETTINGS":
          await a0_0x22efe4();
          _0x36e23e(await getSettings());
          break;
        case "SET_SETTINGS": {
          const _0x3a5bb8 = await setSettings(_0x53dfc7.updates || {});
          a0_0x4723d4(_0x3a5bb8);
          _0x36e23e(_0x3a5bb8);
          break;
        }
        case "TOGGLE_ENABLED": {
          const _0x15b047 = await getSettings();
          if (_0x15b047.enabled) {
            const _0x50880d = await setSettings({
              enabled: false,
            });
            a0_0x4723d4(_0x50880d);
            _0x36e23e(_0x50880d);
            break;
          }
          const _0x5bcda6 = await getLicenseState({
            force: true,
          });
          if (_0x5bcda6.status !== "valid") {
            const _0x1cdbce = await setSettings({
              enabled: false,
            });
            a0_0x4723d4(_0x1cdbce);
            const _0x2371cc = {
              ..._0x1cdbce,
            };
            _0x2371cc.error = _0x5bcda6.error || "Sem licença válida";
            _0x36e23e(_0x2371cc);
            break;
          }
          const [_0x1cd635] = await chrome.tabs.query({
            active: true,
            currentWindow: true,
          });
          const _0x411f19 = _0x1cd635?.url || "";
          const _0x56eaf5 =
            /https:\/\/lovable\.dev\/projects\/[a-f0-9-]+/i.test(_0x411f19);
          if (!_0x1cd635?.id || !_0x56eaf5) {
            _0x36e23e({
              ..._0x15b047,
              error:
                "Abra um projeto Lovable em lovable.dev/projects/<id> antes de ativar.",
            });
            break;
          }
          let _0x5a1407 = false;
          try {
            const _0x1c32d2 = chrome.tabs.sendMessage(_0x1cd635.id, {
              type: "PING",
            });
            const _0x134629 = new Promise((_0x506f76, _0x5252b1) =>
              setTimeout(() => _0x5252b1(new Error("ping-timeout")), 1500),
            );
            const _0x4d05be = await Promise.race([_0x1c32d2, _0x134629]);
            _0x5a1407 = !!_0x4d05be?.ok;
          } catch (_0xe82dbe) {
            _0x5a1407 = false;
          }
          if (!_0x5a1407) {
            const _0x13b356 = {
              ..._0x15b047,
            };
            _0x13b356.error =
              "Recarregue a aba do Lovable (F5) e tente novamente.";
            _0x36e23e(_0x13b356);
            break;
          }
          const _0xe21e7d = await setSettings({
            enabled: true,
          });
          a0_0x4723d4(_0xe21e7d);
          _0x36e23e(_0xe21e7d);
          break;
        }
        case "VALIDATE_LICENSE": {
          const _0x4e8382 = await getSettings();
          const _0x1f51c1 =
            _0x53dfc7.key !== undefined ? _0x53dfc7.key : _0x4e8382.licenseKey;
          const _0x84e9c2 =
            _0x53dfc7.email !== undefined
              ? _0x53dfc7.email
              : _0x4e8382.userEmail;
          if (
            _0x53dfc7.key !== undefined &&
            _0x53dfc7.key !== _0x4e8382.licenseKey
          ) {
            await setSettings({
              licenseKey: _0x1f51c1,
              licenseState: emptyLicenseState(),
            });
          }
          const _0x3111b3 = await validateLicense(_0x1f51c1, _0x84e9c2);
          if (_0x3111b3.status !== "valid") {
            const _0x55497d = await setSettings({
              enabled: false,
            });
            a0_0x4723d4(_0x55497d);
          }
          _0x36e23e(_0x3111b3);
          break;
        }
        case "GET_LICENSE_STATE": {
          const _0x426bcf = {
            force: !!_0x53dfc7.force,
          };
          const _0x43ba18 = await getLicenseState(_0x426bcf);
          _0x36e23e(_0x43ba18);
          break;
        }
        case "CLEAR_LICENSE": {
          await clearLicense();
          const _0x4aec45 = await setSettings({
            enabled: false,
          });
          a0_0x4723d4(_0x4aec45);
          _0x36e23e(_0x4aec45);
          break;
        }
        case "LOG_USER_EMAIL": {
          const _0x4d04e1 = await getSettings();
          const _0x475473 = _0x53dfc7.email || "";
          if (_0x475473 && _0x475473 !== _0x4d04e1.userEmail) {
            const _0x45d204 = {
              userEmail: _0x475473,
            };
            await setSettings(_0x45d204);
            if (_0x4d04e1.licenseKey) {
              validateLicense(_0x4d04e1.licenseKey, _0x475473).catch(() => {});
            }
          }
          _0x36e23e({
            ok: true,
          });
          break;
        }
        case "SAVE_LOVABLE_TOKEN": {
          const _0xa5e56d = _0x53dfc7.token || "";
          if (_0xa5e56d) {
            await setSettings({
              lovableToken: _0xa5e56d,
              lovableTokenAt: Date.now(),
            });
          }
          _0x36e23e({
            ok: true,
          });
          break;
        }
        case "SAVE_LOVABLE_WORKSPACE_ID": {
          const _0x915bcb = _0x53dfc7.workspaceId || "";
          if (_0x915bcb) {
            const _0x3b13f4 = {
              lovableWorkspaceId: _0x915bcb,
            };
            await setSettings(_0x3b13f4);
          }
          _0x36e23e({
            ok: true,
          });
          break;
        }
        case "SAVE_LOVABLE_CASTLE_TOKEN": {
          const _0xa43730 = _0x53dfc7.token || "";
          if (_0xa43730) {
            await setSettings({
              lovableCastleToken: _0xa43730,
              lovableCastleTokenAt: Date.now(),
            });
          }
          _0x36e23e({
            ok: true,
          });
          break;
        }
        case "SAVE_LOVABLE_SESSION_HEADERS": {
          const _0x5c23a6 = {};
          if (_0x53dfc7.sessionId) {
            _0x5c23a6.lovableSessionId = _0x53dfc7.sessionId;
          }
          if (_0x53dfc7.gitSha) {
            _0x5c23a6.lovableClientGitSha = _0x53dfc7.gitSha;
          }
          if (Object.keys(_0x5c23a6).length > 0) {
            await setSettings(_0x5c23a6);
          }
          _0x36e23e({
            ok: true,
          });
          break;
        }
        case "GET_ACTIVE_PROJECT_ID": {
          const _reProj = /https:\/\/lovable\.dev\/projects\/([a-f0-9-]+)/i;
          let [_0x4207a2] = await chrome.tabs.query({
            active: true,
            currentWindow: true,
          });
          let _0x20aeec = _0x4207a2?.url || "";
          let _0x424386 = _reProj.exec(_0x20aeec);
          // SEM fallback: so "conecta" se a ABA ATIVA for o projeto Lovable (igual ao Box antigo).
          // Evita abrir "falsamente conectada" quando ha uma aba do Lovable atras, sem foco.
          const _0xaa0ad1 = {
            projectId: _0x424386 ? _0x424386[1] : null,
            url: _0x20aeec,
          };
          _0x36e23e(_0xaa0ad1);
          break;
        }
        case "CLEAR_CHAT_HISTORY": {
          const _0x296614 = await getSettings();
          const _0x1ba6d0 = {
            ...(_0x296614.tryToFixHistory || {}),
          };
          const _0x302419 = _0x1ba6d0;
          if (_0x53dfc7.projectId) {
            delete _0x302419[_0x53dfc7.projectId];
          } else {
            Object.keys(_0x302419).forEach(
              (_0x10375b) => delete _0x302419[_0x10375b],
            );
          }
          const _0x28d489 = {
            ..._0x296614,
          };
          _0x28d489.tryToFixHistory = _0x302419;
          const _0x3c484f = {
            settings: _0x28d489,
          };
          await chrome.storage.local.set(_0x3c484f);
          const _0x599ef3 = {
            ok: true,
            tryToFixHistory: _0x302419,
          };
          _0x36e23e(_0x599ef3);
          break;
        }
        case "LOG_INTEL": {
          const _0x29f386 = await getSettings();
          const _0x57d7aa = {
            ..._0x29f386.intel,
            ...(_0x53dfc7.data || {}),
          };
          const _0x421eac = {
            intel: _0x57d7aa,
          };
          await setSettings(_0x421eac);
          _0x36e23e({
            ok: true,
          });
          break;
        }
        case "LOG_FEATURE_FLAGS":
          const _0x3f91ab = {
            featureFlags: _0x53dfc7.flags || {},
          };
          await setSettings(_0x3f91ab);
          _0x36e23e({
            ok: true,
          });
          break;
        case "LOG_PROMPT": {
          const _0x1bf6a5 = await getSettings();
          const _0x2770da = new Date().toISOString().slice(0, 10);
          const _0x92505d = {
            ...(_0x1bf6a5.stats?.daily || {}),
          };
          const _0x5bb455 = _0x92505d;
          _0x5bb455[_0x2770da] = (_0x5bb455[_0x2770da] || 0) + 1;
          const _0x20f385 = Object.keys(_0x5bb455).sort();
          if (_0x20f385.length > 30) {
            _0x20f385
              .slice(0, _0x20f385.length - 30)
              .forEach((_0x43ee2d) => delete _0x5bb455[_0x43ee2d]);
          }
          await setSettings({
            stats: {
              ..._0x1bf6a5.stats,
              promptCount: (_0x1bf6a5.stats?.promptCount || 0) + 1,
              lastPromptAt: Date.now(),
              daily: _0x5bb455,
            },
          });
          _0x36e23e({
            ok: true,
          });
          break;
        }
        case "LOG_ERROR": {
          const _0x2c7d16 = await getSettings();
          const _0x162ba7 = {
            ..._0x2c7d16.stats,
          };
          _0x162ba7.errorCount = (_0x2c7d16.stats?.errorCount || 0) + 1;
          const _0x2ce047 = {
            stats: _0x162ba7,
          };
          await setSettings(_0x2ce047);
          _0x36e23e({
            ok: true,
          });
          break;
        }
        case "RESET_STATS": {
          const _0x1a918f = await setSettings({
            stats: {
              promptCount: 0,
              errorCount: 0,
              lastPromptAt: 0,
            },
          });
          _0x36e23e(_0x1a918f);
          break;
        }
        case "OPEN_OPTIONS":
          chrome.runtime.openOptionsPage();
          _0x36e23e({
            ok: true,
          });
          break;
        case "SEND_MESSAGE_PROXY": {
          const _0x197bd0 = String(_0x53dfc7.message || "");
          const _0x1893d7 = String(_0x53dfc7.projectId || "");
          const _0x2a3e07 = String(_0x53dfc7.token || "")
            .replace(/^Bearer\s+/i, "")
            .trim();
          const _0x5e8463 = String(_0x53dfc7.sessionId || "");
          const _0x3a483c = String(_0x53dfc7.gitSha || "");
          if (!_0x2a3e07 || !_0x1893d7 || !_0x197bd0) {
            _0x36e23e({
              ok: false,
              error: "token, projectId ou message ausente",
            });
            break;
          }
          const _0x32cbeb = await new Promise((_0xee7cd2) =>
            chrome.storage.local.get(
              [
                "lovable_chat_payloads",
                "lovable_browser_session_id",
                "lovable_git_sha",
              ],
              _0xee7cd2,
            ),
          );
          const _0x173d2c = _0x32cbeb.lovable_chat_payloads || [];
          const _0x51c73f =
            _0x173d2c.length > 0 ? _0x173d2c[_0x173d2c.length - 1].body : null;
          const _0xb0bdd9 = () => {
            const _0x5c5702 =
              "be_" +
              Date.now() +
              "_" +
              Math.random().toString(36).slice(2, 10);
            // gera um TypeID valido (prefixo_ + base32 estilo ULID) — molde proprio,
            // sem depender de captura da caixa do Lovable
            const _genTypeId = (_p) => {
              const _a = "0123456789abcdefghjkmnpqrstvwxyz";
              let _t = Date.now();
              let _ts = "";
              for (let _i = 0; _i < 10; _i++) {
                _ts = _a[_t % 32] + _ts;
                _t = Math.floor(_t / 32);
              }
              let _r = "";
              for (let _i = 0; _i < 16; _i++) {
                _r += _a[Math.floor(Math.random() * 32)];
              }
              return _p + "_" + _ts + _r;
            };
            const _0x5c348c = {
              id: _genTypeId("umsg"),
              ai_message_id: _genTypeId("aimsg"),
              thread_id: "main",
              chat_only: false,
              intent: "fix_error",
              dispatch_mode: "security_fix",
              source: "ext-input",
              contains_error: true,
              error_ids: [],
              message_intent_metadata: {},
              files: [],
              selected_elements: [],
              optimisticImageUrls: [],
              current_page: "/",
              current_viewport_width: 1200,
              current_viewport_height: 800,
              current_viewport_dpr: 1,
              view: "preview",
              view_description: "The user is currently viewing the preview. ",
              model: null,
              message: _0x197bd0,
              session_replay: "",
              client_logs: [],
              network_requests: [],
              runtime_errors: [],
              integration_metadata: { browser: {} },
            };
            _0x5c348c.message_intent_metadata.fix_error_metadata = {};
            _0x5c348c.message_intent_metadata.fix_error_metadata.errors = [
              {
                error_type: "build",
                error_message: _0x197bd0,
                build_event_id: _0x5c5702,
              },
            ];
            const _0x5c01b0 = _0x5c348c;
            let _0x4240b1 = {};
            if (_0x51c73f && typeof _0x51c73f === "object") {
              const _0x44f5b3 = {
                ..._0x51c73f,
              };
              _0x4240b1 = _0x44f5b3;
              delete _0x4240b1.intent;
              delete _0x4240b1.dispatch_mode;
              delete _0x4240b1.contains_error;
              delete _0x4240b1.error_ids;
              delete _0x4240b1.message_intent_metadata;
            }
            const _0x2d35e1 = {
              ..._0x4240b1,
              ..._0x5c01b0,
            };
            return _0x2d35e1;
          };
          const _0x5c8dec =
            "https://phpsytfgkvepesvljwbu.supabase.co/functions/v1/lov3";
          const _0x4c7740 =
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBocHN5dGZna3ZlcGVzdmxqd2J1Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3ODE3OTk2NzcsImV4cCI6MjA5NzM3NTY3N30.dy1MOzI2nyunLMvOZhR7w1pXbaXY1eXUSjd6WTfG8ps";
          const _0x58f201 = {
            "Content-Type": "application/json",
            apikey: _0x4c7740,
            Authorization: "Bearer " + _0x4c7740,
          };
          const _0x324615 = _0x58f201;
          let _0x55fea0;
          for (let _0x22c7d1 = 0; _0x22c7d1 <= 2; _0x22c7d1++) {
            const _0x24fb30 = new AbortController();
            const _0x3a0c84 = setTimeout(() => _0x24fb30.abort(), 45000);
            try {
              const _0x34f456 = {
                token: _0x2a3e07,
                token_lovable: _0x2a3e07,
                projectId: _0x1893d7,
                projeto_id: _0x1893d7,
                message: _0x197bd0,
                mensagem: _0x197bd0,
                browser_session_id:
                  _0x5e8463 ||
                  _0x32cbeb.lovable_browser_session_id ||
                  undefined,
                lovable_browser_session_id:
                  _0x5e8463 ||
                  _0x32cbeb.lovable_browser_session_id ||
                  undefined,
                client_git_sha:
                  _0x3a483c || _0x32cbeb.lovable_git_sha || undefined,
                lovable_git_sha:
                  _0x3a483c || _0x32cbeb.lovable_git_sha || undefined,
                lastPayload: _0xb0bdd9(),
                mode: "error",
              };
              _0x55fea0 = await fetch(_0x5c8dec, {
                method: "POST",
                headers: _0x324615,
                body: JSON.stringify(_0x34f456),
                signal: _0x24fb30.signal,
              });
              clearTimeout(_0x3a0c84);
              if (_0x55fea0.ok || _0x55fea0.status === 202) {
                break;
              }
              if (_0x55fea0.status === 401 || _0x55fea0.status === 403) {
                break;
              }
              if (_0x22c7d1 === 2) {
                break;
              }
              await new Promise((_0x302b3f) =>
                setTimeout(_0x302b3f, (_0x22c7d1 + 1) * 1500),
              );
            } catch (_0x394514) {
              clearTimeout(_0x3a0c84);
              if (_0x22c7d1 === 2) {
                throw _0x394514;
              }
              await new Promise((_0x244ff1) =>
                setTimeout(_0x244ff1, (_0x22c7d1 + 1) * 1500),
              );
            }
          }
          if (_0x55fea0.ok || _0x55fea0.status === 202) {
            const _0x17f607 = {
              ok: true,
              status: _0x55fea0.status,
            };
            _0x36e23e(_0x17f607);
          } else {
            const _0x5eb723 = await _0x55fea0.text().catch(() => "");
            let _0x7103fc = "Erro " + _0x55fea0.status;
            try {
              _0x7103fc = JSON.parse(_0x5eb723).error || _0x7103fc;
            } catch {}
            const _0x54f976 = {
              ok: false,
              status: _0x55fea0.status,
              error: _0x7103fc,
            };
            _0x36e23e(_0x54f976);
          }
          break;
        }
        default:
          _0x36e23e({
            ok: false,
            error: "unknown: " + _0x53dfc7.type,
          });
      }
    } catch (_0x2c9f56) {
      console.error("[PULSE bg]", _0x2c9f56);
      const _0x248d4f = {
        ok: false,
        error: _0x2c9f56.message,
      };
      _0x36e23e(_0x248d4f);
    }
  })();
  return true;
}
function a0_0x4723d4(_0x2d897a) {
  try {
    const _0x33a10b = !!_0x2d897a?.enabled;
    const _0xa714d2 = _0x2d897a?.licenseState?.status;
    if (_0xa714d2 && _0xa714d2 !== "valid" && _0xa714d2 !== "unknown") {
      chrome.action.setBadgeText({
        text: "!",
      });
      chrome.action.setBadgeBackgroundColor({
        color: "#ef4444",
      });
      chrome.action.setBadgeTextColor?.({
        color: "#ffffff",
      });
      return;
    }
    const _0x3c9a89 = {
      text: "",
    };
    chrome.action.setBadgeText(_0x3c9a89);
    const _0x12c472 = {
      color: _0x33a10b ? "#00CC82" : "#CC2222",
    };
    chrome.action.setBadgeBackgroundColor(_0x12c472);
    chrome.action.setBadgeTextColor?.({
      color: "#ffffff",
    });
  } catch (_0x59dd5d) {}
}
async function a0_0x22efe4() {
  const _0x7db49b = await getSettings();
  const _0x37b07a = _0x7db49b.licenseState?.expiresAt;
  if (!_0x37b07a) {
    return;
  }
  const _0x5e65ea = Date.now();
  const _0x2bde88 = new Date(_0x37b07a).getTime();
  if (Number.isNaN(_0x2bde88)) {
    return;
  }
  if (_0x5e65ea >= _0x2bde88) {
    console.log("[PULSE bg] license expired locally, revoking");
    const _0x16ccd4 = {
      ...emptyLicenseState(),
      status: "expired",
      error: "Licença expirada",
      lastChecked: _0x5e65ea,
    };
    const _0x66e8d3 = {
      enabled: false,
      licenseState: _0x16ccd4,
    };
    const _0x3bffcf = await setSettings(_0x66e8d3);
    a0_0x4723d4(_0x3bffcf);
    return true;
  }
  const _0x4d8a8b = _0x2bde88 - _0x5e65ea;
  if (_0x4d8a8b < 7200000) {
    chrome.alarms?.create("license-expiry-watch", {
      periodInMinutes: 5,
    });
  }
  return false;
}
try {
  chrome.sidePanel
    ?.setPanelBehavior({
      openPanelOnActionClick: true,
    })
    .catch(() => {});
} catch (a0_0x972df1) {}
chrome.runtime.onInstalled.addListener(async () => {
  const _0x4ad753 = await getSettings();
  a0_0x4723d4(_0x4ad753);
  await a0_0x22efe4();
  try {
    chrome.sidePanel?.setPanelBehavior({
      openPanelOnActionClick: true,
    });
  } catch (_0x51b6d3) {}
  if (!_0x4ad753.deviceId) {
    const _0x227576 = crypto.randomUUID();
    const _0x581b33 = {
      deviceId: _0x227576,
    };
    await setSettings(_0x581b33);
    console.log("[LOV] HWID gerado:", _0x227576);
  }
  chrome.alarms?.create("license-revalidate", {
    periodInMinutes: 1,
  });
});
chrome.runtime.onStartup?.addListener(async () => {
  const _0x151490 = await getSettings();
  a0_0x4723d4(_0x151490);
  await a0_0x22efe4();
  try {
    chrome.sidePanel?.setPanelBehavior({
      openPanelOnActionClick: true,
    });
  } catch (_0x3f9fb0) {}
  chrome.alarms?.create("license-revalidate", {
    periodInMinutes: 1,
  });
});
chrome.alarms?.onAlarm.addListener(async (_0x4d1248) => {
  if (_0x4d1248.name === "license-expiry-watch") {
    const _0xd69c6a = await a0_0x22efe4();
    if (_0xd69c6a) {
      chrome.alarms?.clear("license-expiry-watch");
    }
    return;
  }
  if (_0x4d1248.name !== "license-revalidate") {
    return;
  }
  const _0x90be30 = await a0_0x22efe4();
  if (_0x90be30) {
    a0_0x55be9c();
    return;
  }
  const _0x2382e1 = await getSettings();
  if (!_0x2382e1.licenseKey) {
    return;
  }
  const _0x27515c = await validateLicense(
    _0x2382e1.licenseKey,
    _0x2382e1.userEmail,
    _0x2382e1.deviceId,
  ).catch(() => null);
  if (!_0x27515c) {
    return;
  }
  if (_0x27515c.status !== "valid") {
    const _0x1123c5 = {
      enabled: false,
      licenseState: _0x27515c,
    };
    const _0x3f55e9 = await setSettings(_0x1123c5);
    a0_0x4723d4(_0x3f55e9);
    a0_0x55be9c();
    console.log(
      "[LOV] licença inválida detectada no polling:",
      _0x27515c.status,
      _0x27515c.error,
    );
  } else {
    a0_0x4723d4(await getSettings());
  }
});
function a0_0x55be9c() {
  chrome.runtime
    .sendMessage({
      type: "LICENSE_REVOKED",
    })
    .catch(() => {});
}
chrome.commands?.onCommand.addListener(async (_0x3a1c9b) => {
  if (_0x3a1c9b !== "toggle-enabled") {
    return;
  }
  const _0x42badd = await getSettings();
  if (!_0x42badd.enabled) {
    const _0x190bd8 = await getLicenseState({
      force: true,
    });
    if (_0x190bd8.status !== "valid") {
      a0_0x4723d4(
        await setSettings({
          enabled: false,
        }),
      );
      return;
    }
  }
  const _0x455ae0 = {
    enabled: !_0x42badd.enabled,
  };
  a0_0x4723d4(await setSettings(_0x455ae0));
});
try {
  chrome.webRequest.onBeforeRequest.addListener(
    (_0x2c6eca) => {
      if (_0x2c6eca.method !== "POST") {
        return;
      }
      if (!_0x2c6eca.requestBody || !_0x2c6eca.requestBody.raw) {
        return;
      }
      try {
        const _0x3ae437 =
          _0x2c6eca.requestBody.raw[0] && _0x2c6eca.requestBody.raw[0].bytes;
        if (!_0x3ae437) {
          return;
        }
        const _0x449d9e = new TextDecoder("utf-8").decode(_0x3ae437);
        if (!_0x449d9e || _0x449d9e.length < 10) {
          return;
        }
        const _0x427b35 = JSON.parse(_0x449d9e);
        if (!_0x427b35 || typeof _0x427b35 !== "object") {
          return;
        }
        if (
          _0x427b35.ai_message_id &&
          typeof _0x427b35.ai_message_id === "string"
        ) {
          const _0x26ead0 = {
            lovable_last_aimsg: _0x427b35.ai_message_id,
          };
          chrome.storage.local.set(_0x26ead0);
        }
        const _0x158f22 = {
          timestamp: Date.now(),
          url: _0x2c6eca.url,
          body: _0x427b35,
        };
        chrome.storage.local.get(
          {
            lovable_chat_payloads: [],
          },
          (_0x211850) => {
            const _0x259c61 = _0x211850.lovable_chat_payloads || [];
            _0x259c61.push(_0x158f22);
            if (_0x259c61.length > 20) {
              _0x259c61.splice(0, _0x259c61.length - 20);
            }
            const _0x4dc96c = {
              lovable_chat_payloads: _0x259c61,
            };
            chrome.storage.local.set(_0x4dc96c);
          },
        );
      } catch (_0x10fce0) {}
    },
    {
      urls: ["https://api.lovable.dev/projects/*/chat"],
    },
    ["requestBody"],
  );
  console.log(
    "[LOV bg] onBeforeRequest ativo — captura payload nativo do chat",
  );
} catch (a0_0x23e9a2) {
  console.warn("[LOV bg] onBeforeRequest indisponível:", a0_0x23e9a2?.message);
}
