'use strict';

const watchers = require('./watchers-203d7acd.js');
const watchers$1 = require('./watchers-d5742848.js');
const getters = require('./getters-24bb377c.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 = [...getters.availableProcessors().map(({ processor_type }) => processor_type), ...getters.availableManualPaymentMethods().map(({ id }) => id)];
  // selected processor is available.
  if (ids.includes(watchers$1.state.id))
    return;
  // set to first if we have one, otherwise unset.
  watchers$1.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 = (getters.availableMethodTypes() || []).map(({ id }) => id);
  // selected method is available
  if (ids.includes(watchers$1.state.method))
    return;
  // if the current method is not available, set the first method.
  watchers$1.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.
watchers.onChange('checkout', () => {
  maybeUpdateProcessor();
  maybeUpdateMethod();
});
watchers$1.onChange('id', () => maybeUpdateProcessor());
// when processors and methods are first loaded, select first one.
getters.onChange('processors', () => maybeUpdateProcessor());
getters.onChange('methods', () => maybeUpdateMethod());
