'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var hooks = require('preact/hooks');
var errors = require('./errors.js');
var api = require('./api.js');
var subscription = require('./subscription.js');

/**
 * Returns the current array of `metafields` applied to the checkout.
 * You can optionally filter the list.
 * @arg {MetafieldsFilters} - filter the list of returned metafields
 * @deprecated `useMetafields` is deprecated. Use `useAppMetafields` instead.
 */
function useMetafields(filters) {
  const api$1 = api.useApi();
  const extensionTarget = api$1.extension.target;
  if (!('metafields' in api$1)) {
    throw new errors.ExtensionHasNoFieldError('metafields', extensionTarget);
  }
  const metaFields = subscription.useSubscription(api$1.metafields);
  return hooks.useMemo(() => {
    if (filters) {
      const {
        namespace,
        key
      } = filters;
      if (!namespace) {
        throw new errors.CustomerAccountUIExtensionError('You must pass in a namespace with a key');
      }
      const filteredResults = metaFields.filter(metafield => metafield.namespace === namespace && (!key || metafield.key === key));
      return filteredResults;
    }
    return metaFields;
  }, [filters, metaFields]);
}

exports.useMetafields = useMetafields;
