var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

import SimpleSchema from 'simpl-schema';
import SimpleSchema2Bridge from 'uniforms/SimpleSchema2Bridge';
import SelectField from 'uniforms-unstyled/SelectField'; // Choose your theme package.
import LongTextField from 'uniforms-unstyled/LongTextField'; // Choose your theme package

import { SettingTypes } from '../Setting';
import BoolField from './BoolField';

/**
 * @param {SettingDefProps} props
 */
var mapValidatorProps = function mapValidatorProps(props) {
  if (props.validator && props.validator.type === 'regex') {
    return { regEx: new RegExp(props.validator.pattern) };
  }

  return {};
};

export var UniformsSettingsTransformer = function () {
  function UniformsSettingsTransformer() {
    _classCallCheck(this, UniformsSettingsTransformer);
  }

  _createClass(UniformsSettingsTransformer, null, [{
    key: 'defsToSchemaBridge',
    value: function defsToSchemaBridge(definitions) {
      var schema = UniformsSettingsTransformer.defsToSchema(definitions);
      return new SimpleSchema2Bridge(schema);
    }

    /**
     * @param {Array} definitions
     * @return {SimpleSchema}
     */

  }, {
    key: 'defsToSchema',
    value: function defsToSchema(definitions) {
      var mapper = function mapper(definition) {
        var type = SettingTypes.typeOf(definition);

        switch (type) {
          case SettingTypes.TYPE_BOOLEAN:
            return UniformsSettingsTransformer.transformBoolean(definition);
          case SettingTypes.TYPE_TEXT:
            return UniformsSettingsTransformer.transformText(definition);
          case SettingTypes.TYPE_CHOICE:
            return UniformsSettingsTransformer.transformChoice(definition);
          case SettingTypes.TYPE_TEXTAREA:
            return UniformsSettingsTransformer.transformTextarea(definition);
          default:
            return null;
        }
      };

      /**
       * @param {Object} acc
       * @param fragment
       * @return {*}
       */
      var reducer = function reducer(acc, fragment) {
        return Object.assign({}, acc, fragment);
      };

      var mapped = definitions.map(mapper).filter(function (transformed) {
        return !!transformed;
      });

      if (mapped.length !== definitions.length) {
        throw new Error('Could not map definitions');
      }

      return new SimpleSchema(mapped.reduce(reducer, {}));
    }

    /**
     * @param {SettingDefProps} props
     * @return {{}}
     */

  }, {
    key: 'transformChoice',
    value: function transformChoice(props) {
      if (props.multi) {
        var _ref;

        return _ref = {}, _defineProperty(_ref, props.name, {
          type: Array,
          label: props.title,
          defaultValue: props.defaultValue,
          optional: !props.required,
          allowedValues: props.choices.map(function (choice) {
            return choice.value;
          }),
          uniforms: {
            component: SelectField,
            options: props.choices.map(function (choice) {
              return { label: choice.title, value: choice.value };
            })
          }
        }), _defineProperty(_ref, props.name + '.$', {
          type: String
        }), _ref;
      }

      return _defineProperty({}, props.name, {
        type: String,
        label: props.title,
        defaultValue: props.defaultValue,
        optional: !props.required,
        allowedValues: props.choices.map(function (choice) {
          return choice.value;
        }),
        uniforms: {
          component: SelectField,
          options: props.choices.map(function (choice) {
            return { label: choice.title, value: choice.value };
          })
        }
      });
    }

    /**
     * @param props
     * @return {{}}
     */

  }, {
    key: 'transformText',
    value: function transformText(props) {
      var validator = mapValidatorProps(props);

      return _defineProperty({}, props.name, Object.assign({}, validator, {
        label: props.title,
        type: String,
        defaultValue: props.defaultValue,
        optional: !props.required
      }));
    }

    /**
     * @param {SettingDefProps} props
     * @return {{}}
     */

  }, {
    key: 'transformTextarea',
    value: function transformTextarea(props) {
      var validator = mapValidatorProps(props);
      return _defineProperty({}, props.name, Object.assign({}, validator, {
        label: props.title,
        type: String,
        defaultValue: props.defaultValue,
        optional: !props.required,
        uniforms: {
          component: LongTextField
        }
      }));
    }

    /**
     * @param {SettingDefProps} props
     * @return {{}}
     */

  }, {
    key: 'transformBoolean',
    value: function transformBoolean(props) {
      return _defineProperty({}, props.name, {
        type: Boolean,
        label: props.title,
        defaultValue: props.defaultValue,
        optional: !props.required,
        uniforms: {
          component: BoolField
        }
      });
    }
  }]);

  return UniformsSettingsTransformer;
}();