function assert(condition: unknown, message = "assertion failed"): void {
  if (!condition) throw new Error(message);
}

function assertEqual<T>(actual: T, expected: T, message = "values should be equal"): void {
  if (actual !== expected) throw new Error(`${message}: expected ${String(expected)}, got ${String(actual)}`);
}

import { renderToStaticMarkup } from "react-dom/server";
import ResourceAccessExplanation, {
  formatResourceAccessProvenanceDetails,
  resourceAccessProvenanceKindLabel
} from "../src/components/ResourceAccessExplanation";

const loadingMarkup = renderToStaticMarkup(<ResourceAccessExplanation loading />);
assert(
  loadingMarkup.includes("i18n:govoplan-core.loading_access_explanation.04a7c934"),
  "loading copy is owned by Core"
);

const explanationMarkup = renderToStaticMarkup(
  <ResourceAccessExplanation
    fallbackResourceLabel="Fallback resource"
    explanation={{
      user: { id: "user-1", email: "reader@example.test", display_name: "Reader" },
      resource_type: "campaign",
      resource_id: "campaign-1",
      action: "campaigns:campaign:read",
      provenance: [
        {
          kind: "resource",
          id: "campaign-1",
          label: "Quarterly notice",
          source: "campaigns",
          details: { inherited: true, limit: null }
        },
        { kind: "right" }
      ]
    }}
  />
);
assert(explanationMarkup.includes("Reader"), "the preferred user label is rendered");
assert(explanationMarkup.includes("Quarterly notice"), "resource provenance overrides the fallback label");
assert(explanationMarkup.includes("campaigns:campaign:read"), "the evaluated action is rendered");
assert(explanationMarkup.includes("i18n:govoplan-core.permission.2f81a22d"), "known provenance kinds use Core wording");
assert(explanationMarkup.includes("i18n:govoplan-core.no_source.6dcf9723"), "missing sources use Core wording");
assert(!explanationMarkup.includes("govoplan-campaign"), "the shared component does not depend on Campaign wording");
assert(!explanationMarkup.includes("govoplan-files"), "the shared component does not depend on Files wording");

const emptyMarkup = renderToStaticMarkup(
  <ResourceAccessExplanation
    fallbackResourceLabel="Fallback resource"
    explanation={{
      user: { id: "user-1" },
      resource_type: "file",
      resource_id: "file-1",
      action: "files:file:read",
      provenance: []
    }}
  />
);
assert(emptyMarkup.includes("Fallback resource"), "the caller fallback labels resources without resource provenance");
assert(
  emptyMarkup.includes("i18n:govoplan-core.no_access_evidence_was_returned.84a21e4e"),
  "empty evidence uses Core wording"
);

assertEqual(resourceAccessProvenanceKindLabel("custom"), "custom", "unknown provenance kinds remain visible");
assertEqual(
  formatResourceAccessProvenanceDetails({ count: 2, inherited: false, source: null, nested: { id: "role-1" } }),
  'count: 2; inherited: false; source: i18n:govoplan-core.none.6eef6648; nested: {"id":"role-1"}',
  "provenance details retain the established compact format"
);
