'use strict';

const util = require('./util-872b1a55.js');
const index = require('./index-f6f80680.js');
const watchers = require('./watchers-203d7acd.js');

const { state, onChange, on, dispose } = index.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 = () => util.sortByArray(state.processors, 'processor_type', state.sortOrder.processors) // sort.
  .filter(processor => (processor === null || processor === void 0 ? void 0 : processor.live_mode) === ((watchers.state === null || watchers.state === void 0 ? void 0 : watchers.state.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 = watchers.state === null || watchers.state === void 0 ? void 0 : watchers.state.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 = watchers.state === null || watchers.state === void 0 ? void 0 : watchers.state.checkout) === null || _a === void 0 ? void 0 : _a.reusable_payment_method_required)
    ? util.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 = () => util.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; };

exports.availableManualPaymentMethods = availableManualPaymentMethods;
exports.availableMethodTypes = availableMethodTypes;
exports.availableProcessors = availableProcessors;
exports.getAvailableProcessor = getAvailableProcessor;
exports.hasMultipleMethodChoices = hasMultipleMethodChoices;
exports.hasMultipleProcessorChoices = hasMultipleProcessorChoices;
exports.onChange = onChange;
exports.state = state;
