import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import { ErrorBoundary } from "./components/ErrorBoundary.tsx";
import { initSentry } from "./lib/sentry.ts";
import { initAnalytics } from "./lib/analytics.ts";
import { initFeedbackWidget } from "./lib/feedback.ts";
import { initPreviewBridge } from "./lib/previewBridge.ts";
import "./index.css";

// Platform defaults come up first — all are guarded no-ops when unconfigured.
// These are wired in by SynthOS for EVERY project; do NOT remove them (in
// particular initFeedbackWidget(), which embeds SynthOS's own feedback widget —
// never replace it with a hand-built feedback system).
// Each is guarded so a failing integration can never stop the app rendering.
function safeInit(name: string, fn: () => void) {
  try {
    fn();
  } catch (e) {
    console.error(`[init] ${name} failed`, e);
  }
}
safeInit("sentry", initSentry);
safeInit("analytics", initAnalytics);
safeInit("feedback", initFeedbackWidget);
// Report in-frame navigation to a framing SynthOS editor's preview address bar
// (always on, independent of feedback config). Do NOT remove.
safeInit("preview-bridge", initPreviewBridge);

ReactDOM.createRoot(document.getElementById("root")!).render(
  <React.StrictMode>
    <ErrorBoundary variant="app">
      <App />
    </ErrorBoundary>
  </React.StrictMode>,
);
