import assert from 'node:assert/strict';
import test from 'node:test';

import { SceneDarknessStore } from './sceneDarknessStore';

void test('SceneDarknessStore: сохраняет штрихи при переключении сцен', () => {
  const store = new SceneDarknessStore();
  store.switchScene('scene_a', true);
  store.dispatch({
    kind: 'stroke.add',
    stroke: {
      id: 's1',
      seed: 1,
      createdAtMs: 100,
      points: [{ x: 0.5, y: 0.5, tMs: 100 }],
      radiusN: 0.08,
    },
  });

  store.switchScene('scene_b', true);
  assert.equal(store.getState().strokes.length, 0);

  store.switchScene('scene_a', true);
  assert.equal(store.getState().strokes.length, 1);
  assert.equal(store.getState().strokes[0]?.id, 's1');
});

void test('SceneDarknessStore: resetSession очищает кэш', () => {
  const store = new SceneDarknessStore();
  store.switchScene('scene_a', true);
  store.dispatch({
    kind: 'stroke.add',
    stroke: {
      id: 's1',
      seed: 1,
      createdAtMs: 100,
      points: [{ x: 0.2, y: 0.2, tMs: 100 }],
      radiusN: 0.08,
    },
  });
  store.resetSession();
  store.switchScene('scene_a', true);
  assert.equal(store.getState().strokes.length, 0);
});

void test('SceneDarknessStore: draft синхронизируется и сбрасывается при commit', () => {
  const store = new SceneDarknessStore();
  store.switchScene('scene_a', true);
  store.dispatch({
    kind: 'draft.set',
    draft: { points: [{ x: 0.1, y: 0.1, tMs: 1 }], radiusN: 0.05, mode: 'cover' },
  });
  assert.ok(store.getState().draft);
  assert.equal(store.getState().draft?.mode, 'cover');
  store.dispatch({
    kind: 'stroke.add',
    stroke: {
      id: 's1',
      seed: 1,
      createdAtMs: 100,
      points: [{ x: 0.1, y: 0.1, tMs: 1 }],
      radiusN: 0.05,
      mode: 'cover',
    },
  });
  assert.equal(store.getState().draft, null);
  assert.equal(store.getState().strokes.length, 1);
});

void test('SceneDarknessStore: cover-штрих сохраняется в кэше сцены', () => {
  const store = new SceneDarknessStore();
  store.switchScene('scene_a', true);
  store.dispatch({
    kind: 'stroke.add',
    stroke: {
      id: 'open1',
      seed: 1,
      createdAtMs: 100,
      points: [{ x: 0.5, y: 0.5, tMs: 100 }],
      radiusN: 0.08,
      mode: 'reveal',
    },
  });
  store.dispatch({
    kind: 'stroke.add',
    stroke: {
      id: 'close1',
      seed: 2,
      createdAtMs: 200,
      points: [{ x: 0.5, y: 0.5, tMs: 200 }],
      radiusN: 0.08,
      mode: 'cover',
    },
  });
  assert.equal(store.getState().strokes.length, 2);
  assert.equal(store.getState().strokes[1]?.mode, 'cover');
  store.switchScene('scene_b', true);
  store.switchScene('scene_a', true);
  assert.equal(store.getState().strokes.length, 2);
  assert.equal(store.getState().strokes[1]?.mode, 'cover');
});
