import { s as sortByArray } from './util.js';
import { c as createStore } from './index6.js';
import { a as state$1 } from './watchers.js';

const { state, onChange, on, dispose } = createStore({
  processors: [],
  methods: [],
  manualPaymentMethods: [],
  disabled: {
    processors: [],
  },
  sortOrder: {
    processors: ['stripe', 'paypal'],
    manualPaymentMethods: [],
    paymentMethods: {
      mollie: ['creditcard', 'paypal'],
    },
  },
  config: {
    stripe: {
      paymentElement: false,
    },
  },
}, (newValue, oldValue) => {
  return JSON.stringify(newValue) !== JSON.stringify(oldValue);
});

/**
 * Gets a sorted array of available processors based on
 * checkout mode, recurring requirements, and if mollie is enabled.
 */
const availableProcessors = () => sortByArray(state.processors, 'processor_type', state.sortOrder.processors) // sort.
  .filter(processor => (processor === null || processor === void 0 ? void 0 : processor.live_mode) === ((state$1 === null || state$1 === void 0 ? void 0 : state$1.mode) === 'live')) // match mode.
  .filter(processor => !(state.disabled.processors || []).includes(processor.processor_type)) // make sure it's not disabled.
  .filter(processor => { var _a; return (!!((_a = state$1 === null || state$1 === void 0 ? void 0 : state$1.checkout) === null || _a === void 0 ? void 0 : _a.reusable_payment_method_required) ? !!(processor === null || processor === void 0 ? void 0 : processor.recurring_enabled) : true); }) // recurring.
  .filter((processor, _, filtered) => (filtered.some(p => p.processor_type === 'mollie') ? processor.processor_type === 'mollie' : true)); // only allow mollie if preset.
/**
 * Gets an available processor type.
 */
const getAvailableProcessor = (type) => availableProcessors().find(({ processor_type }) => processor_type === type);
/**
 * Get a sorted array of manual payment methods
 * based on recurring requirements.
 */
const availableManualPaymentMethods = () => {
  var _a;
  return !((_a = state$1 === null || state$1 === void 0 ? void 0 : state$1.checkout) === null || _a === void 0 ? void 0 : _a.reusable_payment_method_required)
    ? sortByArray(state.manualPaymentMethods, 'id', state.sortOrder.manualPaymentMethods).filter(processor => !(state.disabled.processors || []).includes(processor === null || processor === void 0 ? void 0 : processor.id))
    : [];
};
/**
 * Get a sorted array of mollie payment method types.
 */
const availableMethodTypes = () => sortByArray(state.methods, 'id', state.sortOrder.paymentMethods.mollie);
/**
 * Get a combined available processor choices (processors + manual payment methods)
 */
const availableProcessorChoices = () => [...availableProcessors(), ...availableManualPaymentMethods()];
/**
 * Do we have multiple processors.
 */
const hasMultipleProcessorChoices = () => { var _a; return ((_a = availableProcessorChoices()) === null || _a === void 0 ? void 0 : _a.length) > 1; };
/**
 * Get a combined available payment methods (method types + manual payment methods)
 */
const availableMethodChoices = () => [...availableMethodTypes(), ...availableManualPaymentMethods()];
/**
 * Do we have multiple payment methods.
 */
const hasMultipleMethodChoices = () => { var _a; return ((_a = availableMethodChoices()) === null || _a === void 0 ? void 0 : _a.length) > 1; };

export { availableProcessors as a, availableManualPaymentMethods as b, availableMethodTypes as c, hasMultipleMethodChoices as d, getAvailableProcessor as g, hasMultipleProcessorChoices as h, onChange as o, state as s };
