import { CustomerAccountUIExtensionError } from './errors.mjs';
import { useMetafields } from './metafields.mjs';

/**
 * Returns a single filtered `Metafield` or `undefined`.
 * @arg {MetafieldFilter} - filter the list of returned metafields to a single metafield
 */
function useMetafield(filters) {
  const {
    namespace,
    key
  } = filters;
  if (!namespace || !key) {
    throw new CustomerAccountUIExtensionError('You must pass in both a namespace and key');
  }
  const metafields = useMetafields({
    namespace,
    key
  });
  return metafields.length ? metafields[0] : undefined;
}

export { useMetafield };
