import { test, expect } from "bun:test";
import { parseComponentProps, resolveTProps } from "../src/runtime.ts";

test("parseComponentProps extracts quoted attrs, skips data-component", () => {
  const props = parseComponentProps(
    'data-component="badge" label="New" count="3" disabled autofocus',
  );
  expect(props).toEqual({ label: "New", count: "3", disabled: "", autofocus: "" });
});

test("parseComponentProps decodes compiler-escaped structured props", () => {
  const props = parseComponentProps(
    'data-component="list" items="[{&amp;quot;label&amp;quot;:&amp;quot;First&amp;quot;}]"',
  );
  expect(props.items).toBe('[{"label":"First"}]');
  expect(JSON.parse(props.items!)).toEqual([{ label: "First" }]);
});

test("resolveTProps translates {t:key} markers via the active language", () => {
  const t = (key: string) => ({ "status.new": "Nuevo", greeting: "Hola" })[key] ?? key;
  const props = resolveTProps({ label: "{t:status.new}", plain: "as-is", hi: "{t:greeting}!" }, t);
  expect(props).toEqual({ label: "Nuevo", plain: "as-is", hi: "Hola!" });
});

test("resolveTProps leaves values without markers untouched", () => {
  const props = resolveTProps({ a: "1", b: "hello" }, (k) => `T(${k})`);
  expect(props).toEqual({ a: "1", b: "hello" });
});
