import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createStore } from 'jotai';
import { enableMapSet } from 'immer';
import { makeClosedNavCategoriesAtom, makeNavCategoryId } from './closedNavCategories';

// makeClosedNavCategoriesAtom(userId) builds a Set<string> atom whose reducer uses
// immer produce (PUT add / DELETE delete) and persists to a per-user localStorage
// key `closedNavCategories<userId>`. The reducers produce over a Set, so we enable
// immer's Map/Set plugin (the app does this once at startup).
// makeNavCategoryId joins args with '|'.
enableMapSet();

// In-memory localStorage so atomWithLocalStorage can read/write at construction
// and on set. window is referenced for the onMount storage listener.
type Store = Record<string, string>;
const installLocalStorage = (): Store => {
  const data: Store = {};
  const ls = {
    getItem: (k: string) => (k in data ? data[k] : null),
    setItem: (k: string, v: string) => {
      data[k] = String(v);
    },
    removeItem: (k: string) => {
      delete data[k];
    },
  };
  (globalThis as { localStorage?: unknown }).localStorage = ls;
  (globalThis as { window?: unknown }).window = {
    addEventListener: () => undefined,
    removeEventListener: () => undefined,
  };
  return data;
};

test('makeNavCategoryId joins args with "|"', () => {
  assert.equal(makeNavCategoryId('a', 'b', 'c'), 'a|b|c');
  assert.equal(makeNavCategoryId('only'), 'only');
  assert.equal(makeNavCategoryId(), '');
});

test('starts empty when nothing is stored', () => {
  installLocalStorage();
  const store = createStore();
  const navAtom = makeClosedNavCategoriesAtom('@u:server');
  assert.equal(store.get(navAtom).size, 0);
});

test('hydrates the Set from the per-user localStorage key', () => {
  const data = installLocalStorage();
  data['closedNavCategories@u:server'] = JSON.stringify(['x', 'y']);
  const store = createStore();
  const navAtom = makeClosedNavCategoriesAtom('@u:server');
  assert.deepEqual(Array.from(store.get(navAtom)).sort(), ['x', 'y']);
});

test('PUT adds a category and DELETE removes it', () => {
  installLocalStorage();
  const store = createStore();
  const navAtom = makeClosedNavCategoriesAtom('@u:server');

  store.set(navAtom, { type: 'PUT', categoryId: 'cat1' });
  assert.deepEqual(Array.from(store.get(navAtom)), ['cat1']);

  store.set(navAtom, { type: 'DELETE', categoryId: 'cat1' });
  assert.equal(store.get(navAtom).has('cat1'), false);
});

test('PUT of an existing category keeps the Set unchanged (idempotent)', () => {
  installLocalStorage();
  const store = createStore();
  const navAtom = makeClosedNavCategoriesAtom('@u:server');
  store.set(navAtom, { type: 'PUT', categoryId: 'cat1' });
  store.set(navAtom, { type: 'PUT', categoryId: 'cat1' });
  assert.equal(store.get(navAtom).size, 1);
});

test('DELETE of an absent category is a no-op', () => {
  installLocalStorage();
  const store = createStore();
  const navAtom = makeClosedNavCategoriesAtom('@u:server');
  store.set(navAtom, { type: 'DELETE', categoryId: 'missing' });
  assert.equal(store.get(navAtom).size, 0);
});

test('writes persist to localStorage as an array', () => {
  const data = installLocalStorage();
  const store = createStore();
  const navAtom = makeClosedNavCategoriesAtom('@u:server');
  store.set(navAtom, { type: 'PUT', categoryId: 'cat1' });
  assert.deepEqual(JSON.parse(data['closedNavCategories@u:server']), ['cat1']);
});

test('the storage key is namespaced per user', () => {
  const data = installLocalStorage();
  const store = createStore();
  const aAtom = makeClosedNavCategoriesAtom('@a:server');
  const bAtom = makeClosedNavCategoriesAtom('@b:server');

  store.set(aAtom, { type: 'PUT', categoryId: 'only-a' });
  assert.deepEqual(JSON.parse(data['closedNavCategories@a:server']), ['only-a']);
  assert.equal(data['closedNavCategories@b:server'], undefined);
  assert.equal(store.get(bAtom).size, 0);
});
