import test from "node:test";
import { strictEqual, deepStrictEqual } from "node:assert";

const componentA = () => {
  const state: { key: "a"; current: null | number } = {
    key: "a",
    current: null,
  };

  return {
    state,
    api: {
      increment() {
        state.current ? (state.current += 1) : 1;
      },
    },
  };
};

const componentB = () => {
  const state: { key: "b"; current: number } = { key: "b", current: 0 };

  return {
    state,
    api: {
      add(n: number) {
        state.current += n;
      },
    },
  };
};

function compose() {
  const a = componentA();
  const b = componentB();

  const initialState = {
    [a.state.key]: a.state,
    [b.state.key]: b.state,
  };

  return {
    api: {
      ...a.api,
      ...b.api,
    },
    state: new Proxy(initialState, {
      get(target, prop) {},
    }),
  };
}

test("compose", async (t) => {
  await t.test("composes thing", () => {
    const composition = compose();
    strictEqual(composition.state.a, null);
    strictEqual(composition.state.b, 0);
    composition.api.increment();
    strictEqual(composition.state.a, 1);
  });
});
