'use strict';

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

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

/**
 * Returns the current localized fields and
 * re-renders your component if the values change.
 */
function useLocalizedFields(keys) {
  const {
    localizedFields
  } = api.useApi();
  if (!localizedFields) {
    throw new errors.ScopeNotGrantedError('Using localized fields requires having personal customer data permissions granted to your app.');
  }
  const localizedFieldsData = subscription.useSubscription(localizedFields);
  if (!keys) {
    return localizedFieldsData;
  }
  if (!keys.length) {
    return [];
  }
  return localizedFieldsData.filter(({
    key
  }) => keys.includes(key));
}

/**
 * Returns the current localized field or undefined for the specified
 * localized field key and re-renders your component if the value changes.
 */
function useLocalizedField(key) {
  const fields = useLocalizedFields([key]);
  return fields[0];
}

exports.useLocalizedField = useLocalizedField;
exports.useLocalizedFields = useLocalizedFields;
