import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import ts from "typescript";

const source = await readFile(new URL("../apps/catalog/src/mapPresentationProfile.ts", import.meta.url), "utf8");
const transpiled = ts.transpileModule(source, {
  compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 },
}).outputText;
const moduleUrl = `data:text/javascript;base64,${Buffer.from(transpiled).toString("base64")}`;
const {
  mapFactMatchesFilters,
  mapPresentationBindingIsAll,
  mapPresentationFacetCounts,
  toggleMapPresentationFacetSelection,
} = await import(moduleUrl);

const profile = {
  id: "map.moving-object.operational.default",
  facets: [
    {
      field: "signal_state",
      values: [{ value: "active" }, { value: "inactive" }],
    },
    {
      field: "movement_state",
      values: [{ value: "moving" }, { value: "stopped" }],
    },
  ],
};
const onlineMoving = {
  attributes: { signal_state: "active", movement_state: "moving" },
};

test("missing subject state is explicit all for that binding", () => {
  assert.equal(mapPresentationBindingIsAll("fleet", {}), true);
  assert.equal(mapFactMatchesFilters(onlineMoving, profile, {}, "fleet"), true);
});

test("a selected chip restricts only its own facet", () => {
  const filters = { fleet: { visible: true, facets: { signal_state: ["active"] } } };
  assert.equal(mapFactMatchesFilters(onlineMoving, profile, filters, "fleet"), true);
  assert.equal(mapFactMatchesFilters({ attributes: { signal_state: "inactive", movement_state: "moving" } }, profile, filters, "fleet"), false);
});

test("deselecting the last chip remains empty and never normalizes to all", () => {
  const filters = { fleet: { visible: true, facets: { signal_state: [] } } };
  assert.equal(mapPresentationBindingIsAll("fleet", filters), false);
  assert.equal(mapFactMatchesFilters(onlineMoving, profile, filters, "fleet"), false);
});

test("interactive deselect of the last chip removes the facet constraint", () => {
  const selected = toggleMapPresentationFacetSelection({}, "signal_state", "active");
  assert.deepEqual(selected, { signal_state: ["active"] });

  const unconstrained = toggleMapPresentationFacetSelection(selected, "signal_state", "active");
  assert.deepEqual(unconstrained, {});
  assert.equal(mapPresentationBindingIsAll("fleet", { fleet: { visible: true, facets: unconstrained } }), true);
});

test("interactive deselect preserves other values and facet constraints", () => {
  const facets = {
    signal_state: ["active", "inactive"],
    movement_state: ["moving"],
  };
  assert.deepEqual(toggleMapPresentationFacetSelection(facets, "signal_state", "active"), {
    signal_state: ["inactive"],
    movement_state: ["moving"],
  });
});

test("chips from different facets form one global union", () => {
  const filters = {
    fleet: {
      visible: true,
      facets: {
        signal_state: ["active"],
        movement_state: ["moving"],
      },
    },
  };

  assert.equal(mapFactMatchesFilters(onlineMoving, profile, filters, "fleet"), true);
  assert.equal(mapFactMatchesFilters({ attributes: { signal_state: "active", movement_state: "stopped" } }, profile, filters, "fleet"), true);
  assert.equal(mapFactMatchesFilters({ attributes: { signal_state: "inactive", movement_state: "moving" } }, profile, filters, "fleet"), false);
});

test("movement filters and counters ignore stale movement on inactive subjects", () => {
  const activeStopped = { attributes: { signal_state: "active", movement_state: "stopped" } };
  const inactiveMoving = { attributes: { signal_state: "inactive", movement_state: "moving" } };
  const inactiveStopped = { attributes: { signal_state: "inactive", movement_state: "stopped" } };
  const filters = { fleet: { visible: true, facets: { movement_state: ["moving"] } } };

  assert.equal(mapFactMatchesFilters(onlineMoving, profile, filters, "fleet"), true);
  assert.equal(mapFactMatchesFilters(inactiveMoving, profile, filters, "fleet"), false);

  assert.deepEqual(mapPresentationFacetCounts([
    onlineMoving,
    activeStopped,
    inactiveMoving,
    inactiveStopped,
  ], profile), {
    signal_state: { active: 2, inactive: 2 },
    movement_state: { moving: 1, stopped: 1 },
  });
});

test("explicitly disabled All hides only the selected binding", () => {
  const filters = {
    fleet: { visible: false, facets: {} },
    service: { visible: true, facets: {} },
  };
  assert.equal(mapFactMatchesFilters(onlineMoving, profile, filters, "fleet"), false);
  assert.equal(mapFactMatchesFilters(onlineMoving, profile, filters, "service"), true);
});
