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

import { EffectsStore } from './effectsStore';

void test('defaultTool: при старте инструмент не выбран', () => {
  const store = new EffectsStore();
  assert.equal(store.getState().tool.tool, 'none');
});

void test('pruneExpired: лёд не удаляется по времени', () => {
  const store = new EffectsStore();
  const createdAtMs = Date.now() - 365 * 24 * 60 * 60 * 1000;
  store.dispatch({
    kind: 'instance.add',
    instance: {
      id: 'ice_test',
      type: 'ice',
      seed: 1,
      createdAtMs,
      at: { x: 0.5, y: 0.5 },
      radiusN: 0.1,
      opacity: 0.85,
      lifetimeMs: null,
    },
  });
  assert.equal(store.getState().instances.length, 1);
  assert.equal(store.pruneExpired(), false);
  assert.equal(store.getState().instances.length, 1);
});

void test('pruneExpired: молния удаляется после lifetime', () => {
  const store = new EffectsStore();
  store.dispatch({
    kind: 'instance.add',
    instance: {
      id: 'lt_test',
      type: 'lightning',
      seed: 1,
      createdAtMs: Date.now() - 10_000,
      start: { x: 0, y: 0 },
      end: { x: 0.5, y: 0.5 },
      widthN: 0.05,
      intensity: 1,
      lifetimeMs: 1000,
    },
  });
  assert.equal(store.pruneExpired(), true);
  assert.equal(store.getState().instances.length, 0);
});

void test('pruneExpired: луч света удаляется после lifetime', () => {
  const store = new EffectsStore();
  store.dispatch({
    kind: 'instance.add',
    instance: {
      id: 'sb_test',
      type: 'sunbeam',
      seed: 1,
      createdAtMs: Date.now() - 10_000,
      start: { x: 0.5, y: 0 },
      end: { x: 0.5, y: 0.6 },
      widthN: 0.04,
      intensity: 1,
      lifetimeMs: 800,
    },
  });
  assert.equal(store.pruneExpired(), true);
  assert.equal(store.getState().instances.length, 0);
});

void test('pruneExpired: яд удаляется после lifetime', () => {
  const store = new EffectsStore();
  store.dispatch({
    kind: 'instance.add',
    instance: {
      id: 'pc_test',
      type: 'poisonCloud',
      seed: 1,
      createdAtMs: Date.now() - 20_000,
      at: { x: 0.5, y: 0.5 },
      radiusN: 0.08,
      intensity: 1,
      lifetimeMs: 3000,
    },
  });
  assert.equal(store.pruneExpired(), true);
  assert.equal(store.getState().instances.length, 0);
});

void test('pruneExpired: взрыв удаляется после lifetime', () => {
  const store = new EffectsStore();
  store.dispatch({
    kind: 'instance.add',
    instance: {
      id: 'ex_test',
      type: 'explosion',
      seed: 1,
      createdAtMs: Date.now() - 20_000,
      at: { x: 0.5, y: 0.5 },
      radiusN: 0.08,
      intensity: 1,
      lifetimeMs: 4200,
    },
  });
  assert.equal(store.pruneExpired(), true);
  assert.equal(store.getState().instances.length, 0);
});
