define(['i18n', 'ui/generis/widget/widget', 'handlebars', 'lib/handlebars/helpers'], function (__, widgetFactory, Handlebars, Helpers0) { 'use strict';

    __ = __ && Object.prototype.hasOwnProperty.call(__, 'default') ? __['default'] : __;
    widgetFactory = widgetFactory && Object.prototype.hasOwnProperty.call(widgetFactory, 'default') ? widgetFactory['default'] : widgetFactory;
    Handlebars = Handlebars && Object.prototype.hasOwnProperty.call(Handlebars, 'default') ? Handlebars['default'] : Handlebars;
    Helpers0 = Helpers0 && Object.prototype.hasOwnProperty.call(Helpers0, 'default') ? Helpers0['default'] : Helpers0;

    if (!Helpers0.__initialized) {
      Helpers0(Handlebars);
      Helpers0.__initialized = true;
    }
    var Template = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
      this.compilerInfo = [4,'>= 1.0.0'];
    helpers = this.merge(helpers, Handlebars.helpers); partials = this.merge(partials, Handlebars.partials); data = data || {};
      var buffer = "", stack1, helper, self=this, functionType="function", escapeExpression=this.escapeExpression;


      buffer += "<div class=\"ui-generis-widget text-box\">\n    <div class=\"left\">\n        ";
      stack1 = self.invokePartial(partials['ui-generis-widget-label'], 'ui-generis-widget-label', depth0, helpers, partials, data);
      if(stack1 || stack1 === 0) { buffer += stack1; }
      buffer += "\n    </div>\n    <div class=\"right\">\n        <input name=\"";
      if (helper = helpers.uri) { stack1 = helper.call(depth0, {hash:{},data:data}); }
      else { helper = (depth0 && depth0.uri); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
      buffer += escapeExpression(stack1)
        + "\" value=\"";
      if (helper = helpers.value) { stack1 = helper.call(depth0, {hash:{},data:data}); }
      else { helper = (depth0 && depth0.value); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
      buffer += escapeExpression(stack1)
        + "\">\n    </div>\n</div>";
      return buffer;
      });
    function tpl(data, options, asString) {
      var html = Template(data, options);
      return (asString || true) ? html : $(html);
    }

    /**
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * as published by the Free Software Foundation; under version 2
     * of the License (non-upgradable).
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     *
     * Copyright (c) 2017 (original work) Open Assessment Technologies SA;
     */

    /**
     * The factory
     * @param {Object[]} [options.validator]
     * @param {String} config.label
     * @param {String} [confgi.required = false]
     * @param {String} config.uri
     * @param {String} [config.value = '']
     * @returns {ui/component}
     */
    function factory(options, config) {
      var validator = options.validator || [];
      var widget;
      widget = widgetFactory({
        validator: validator
      }, {
        // no spec overrides
      }).setTemplate(tpl).init({
        label: config.label,
        required: config.required || false,
        uri: config.uri,
        value: config.value || ''
      });

      // Validations
      if (widget.config.required) {
        widget.validator.addValidation({
          message: __('This field is required'),
          predicate: /\S+/,
          precedence: 1
        });
      }
      return widget;
    }

    return factory;

});
