"use strict";

const test = require("node:test");
const assert = require("node:assert/strict");
const selection = require("../../src/MBN_STOCK_WEBVIEW.LegacyParityApp/Web/legacy-named-playlist-selection.js");

const first = "named-definition-first";
const second = "named-definition-second";

test("a second-row click blocks actions until native state selects that exact row", () => {
  const workflow = selection.create();

  const started = workflow.begin(second, first, "named-ui-1", 1);

  assert.deepEqual(started, {
    accepted: true,
    shouldSend: true,
    definitionId: second,
    requestId: "named-ui-1"
  });
  assert.equal(workflow.isPending(), true);
  assert.equal(workflow.pendingDefinitionId(), second);
  assert.deepEqual(workflow.reconcile({
    sequence: 1,
    command: "refresh-named-playlists",
    targetId: "named-ui-old",
    succeeded: true
  }, first), {
    completed: false,
    succeeded: false,
    deferredAction: null
  });
  assert.equal(workflow.isPending(), true);

  const completed = workflow.reconcile({
    sequence: 1,
    command: "select-named-playlist",
    targetId: "named-ui-1",
    succeeded: true
  }, second);

  assert.equal(completed.completed, true);
  assert.equal(completed.succeeded, true);
  assert.equal(workflow.isPending(), false);
});

test("repeated click and double click queue one exact action without a duplicate select", () => {
  const workflow = selection.create();
  assert.equal(workflow.begin(second, first, "named-ui-2", 2).shouldSend, true);

  const repeated = workflow.begin(second, first);
  const deferred = Object.freeze({ definitionId: second, mode: "save" });

  assert.equal(repeated.accepted, true);
  assert.equal(repeated.shouldSend, false);
  assert.equal(repeated.requestId, "named-ui-2");
  assert.equal(workflow.defer(second, deferred), true);
  const completed = workflow.reconcile({
    sequence: 2,
    command: "select-named-playlist",
    targetId: "named-ui-2",
    succeeded: true
  }, second);
  assert.deepEqual(completed.deferredAction, deferred);
  assert.equal(completed.succeeded, true);
});

test("a failed native selection clears the lock and discards the deferred mutation", () => {
  const workflow = selection.create();
  workflow.begin(second, first, "named-ui-3", 3);
  workflow.defer(second, { definitionId: second, mode: "load" });

  const failed = workflow.reconcile({
    sequence: 3,
    command: "select-named-playlist",
    targetId: "named-ui-3",
    succeeded: false
  }, first);

  assert.deepEqual(failed, {
    completed: true,
    succeeded: false,
    deferredAction: null
  });
  assert.equal(workflow.isPending(), false);
});

test("an already selected row never creates a pending native request", () => {
  const workflow = selection.create();

  const current = workflow.begin(first, first);

  assert.equal(current.accepted, true);
  assert.equal(current.shouldSend, false);
  assert.equal(current.requestId, null);
  assert.equal(workflow.isPending(), false);
});

test("a different row cannot replace an in-flight selection target", () => {
  const workflow = selection.create();
  workflow.begin(second, first, "named-ui-4", 4);

  const conflicting = workflow.begin(first, first);

  assert.equal(conflicting.accepted, false);
  assert.equal(conflicting.shouldSend, false);
  assert.equal(workflow.pendingDefinitionId(), second);
});
