import { o as onChange } from './watchers-0c79cf4e.js';
import { o as onChange$1, s as state } from './watchers-e86a5dda.js';
import { o as onChange$2, a as availableProcessors, b as availableManualPaymentMethods, c as availableMethodTypes } from './getters-a2fa2242.js';

/**
 * Look through available processors and maybe switch if the processor has been removed.
 */
const maybeUpdateProcessor = () => {
  // get array of manual and regular processors ids.
  const ids = [...availableProcessors().map(({ processor_type }) => processor_type), ...availableManualPaymentMethods().map(({ id }) => id)];
  // selected processor is available.
  if (ids.includes(state.id))
    return;
  // set to first if we have one, otherwise unset.
  state.id = (ids === null || ids === void 0 ? void 0 : ids.length) ? ids === null || ids === void 0 ? void 0 : ids[0] : null;
};
/**
 * Look through available methods and maybe switch if the processor has been removed.
 */
const maybeUpdateMethod = () => {
  // get method ids.
  const ids = (availableMethodTypes() || []).map(({ id }) => id);
  // selected method is available
  if (ids.includes(state.method))
    return;
  // if the current method is not available, set the first method.
  state.method = (ids === null || ids === void 0 ? void 0 : ids.length) ? ids === null || ids === void 0 ? void 0 : ids[0] : null;
};
// when the checkout changes, maybe update selection.
onChange('checkout', () => {
  maybeUpdateProcessor();
  maybeUpdateMethod();
});
onChange$1('id', () => maybeUpdateProcessor());
// when processors and methods are first loaded, select first one.
onChange$2('processors', () => maybeUpdateProcessor());
onChange$2('methods', () => maybeUpdateMethod());
