(function (root, factory) {
  const api = factory();
  if (typeof module === "object" && module.exports) module.exports = api;
  if (root) root.LegacyNamedPlaylistSelection = api;
})(typeof globalThis !== "undefined" ? globalThis : this, function () {
  "use strict";

  function requireDefinitionId(value) {
    if (typeof value !== "string" || value.length === 0) {
      throw new TypeError("A named-playlist definition id is required.");
    }
    return value;
  }

  function create() {
    let pending = null;

    function begin(
      definitionId,
      selectedDefinitionId,
      requestId,
      minimumSequence
    ) {
      const targetId = requireDefinitionId(definitionId);
      if (pending) {
        return {
          accepted: pending.definitionId === targetId,
          shouldSend: false,
          definitionId: pending.definitionId,
          requestId: pending.requestId
        };
      }

      if (selectedDefinitionId === targetId) {
        return {
          accepted: true,
          shouldSend: false,
          definitionId: targetId,
          requestId: null
        };
      }

      const correlationId = requireDefinitionId(requestId);
      if (!Number.isSafeInteger(minimumSequence) || minimumSequence < 1) {
        throw new TypeError("A positive minimum command sequence is required.");
      }
      pending = {
        definitionId: targetId,
        requestId: correlationId,
        minimumSequence: minimumSequence,
        deferredAction: null
      };
      return {
        accepted: true,
        shouldSend: true,
        definitionId: targetId,
        requestId: correlationId
      };
    }

    function defer(definitionId, action) {
      const targetId = requireDefinitionId(definitionId);
      if (!pending || pending.definitionId !== targetId || !action) return false;
      pending.deferredAction = Object.freeze(Object.assign({}, action));
      return true;
    }

    function reconcile(commandResult, selectedDefinitionId) {
      if (!pending) {
        return { completed: false, succeeded: false, deferredAction: null };
      }

      const exact = !!commandResult &&
        Number.isSafeInteger(commandResult.sequence) &&
        commandResult.sequence >= pending.minimumSequence &&
        commandResult.command === "select-named-playlist" &&
        commandResult.targetId === pending.requestId;
      if (!exact) {
        return { completed: false, succeeded: false, deferredAction: null };
      }

      const succeeded = commandResult.succeeded === true &&
        selectedDefinitionId === pending.definitionId;
      const deferredAction = succeeded ? pending.deferredAction : null;
      pending = null;
      return {
        completed: true,
        succeeded: succeeded,
        deferredAction: deferredAction
      };
    }

    function cancel() {
      pending = null;
    }

    return Object.freeze({
      begin: begin,
      defer: defer,
      reconcile: reconcile,
      cancel: cancel,
      isPending: function () { return pending !== null; },
      pendingDefinitionId: function () {
        return pending ? pending.definitionId : null;
      }
    });
  }

  return Object.freeze({ create: create });
});
