define(['jquery', 'lodash', 'i18n', 'ui/component', 'lib/uuid', 'handlebars', 'lib/handlebars/helpers', 'css!ui/switch/css/switch.css'], function ($$1, _, __, component, uuid, Handlebars, Helpers0, switch_css) { 'use strict';

    $$1 = $$1 && Object.prototype.hasOwnProperty.call($$1, 'default') ? $$1['default'] : $$1;
    _ = _ && Object.prototype.hasOwnProperty.call(_, 'default') ? _['default'] : _;
    __ = __ && Object.prototype.hasOwnProperty.call(__, 'default') ? __['default'] : __;
    component = component && Object.prototype.hasOwnProperty.call(component, 'default') ? component['default'] : component;
    uuid = uuid && Object.prototype.hasOwnProperty.call(uuid, 'default') ? uuid['default'] : uuid;
    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); data = data || {};
      var buffer = "", stack1, helper, self=this, functionType="function", escapeExpression=this.escapeExpression;

    function program1(depth0,data) {
      
      
      return "monostyle";
      }

    function program3(depth0,data) {
      
      
      return "checked";
      }

    function program5(depth0,data) {
      
      
      return "active";
      }

      buffer += "<div class=\"switch ";
      stack1 = helpers['if'].call(depth0, (depth0 && depth0.monoStyle), {hash:{},inverse:self.noop,fn:self.program(1, program1, data),data:data});
      if(stack1 || stack1 === 0) { buffer += stack1; }
      buffer += "\" title=\"";
      if (helper = helpers.title) { stack1 = helper.call(depth0, {hash:{},data:data}); }
      else { helper = (depth0 && depth0.title); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
      buffer += escapeExpression(stack1)
        + "\">\n    <input type=\"checkbox\" name=\"";
      if (helper = helpers.name) { stack1 = helper.call(depth0, {hash:{},data:data}); }
      else { helper = (depth0 && depth0.name); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
      buffer += escapeExpression(stack1)
        + "\" ";
      stack1 = helpers['if'].call(depth0, ((stack1 = (depth0 && depth0.on)),stack1 == null || stack1 === false ? stack1 : stack1.active), {hash:{},inverse:self.noop,fn:self.program(3, program3, data),data:data});
      if(stack1 || stack1 === 0) { buffer += stack1; }
      buffer += ">\n    <label>\n        <span class=\"off ";
      stack1 = helpers['if'].call(depth0, ((stack1 = (depth0 && depth0.off)),stack1 == null || stack1 === false ? stack1 : stack1.active), {hash:{},inverse:self.noop,fn:self.program(5, program5, data),data:data});
      if(stack1 || stack1 === 0) { buffer += stack1; }
      buffer += "\">"
        + escapeExpression(((stack1 = ((stack1 = (depth0 && depth0.off)),stack1 == null || stack1 === false ? stack1 : stack1.label)),typeof stack1 === functionType ? stack1.apply(depth0) : stack1))
        + "</span>\n        <span class=\"on  ";
      stack1 = helpers['if'].call(depth0, ((stack1 = (depth0 && depth0.on)),stack1 == null || stack1 === false ? stack1 : stack1.active), {hash:{},inverse:self.noop,fn:self.program(5, program5, data),data:data});
      if(stack1 || stack1 === 0) { buffer += stack1; }
      buffer += "\">"
        + escapeExpression(((stack1 = ((stack1 = (depth0 && depth0.on)),stack1 == null || stack1 === false ? stack1 : stack1.label)),typeof stack1 === functionType ? stack1.apply(depth0) : stack1))
        + "</span>\n    </label>\n</div>\n";
      return buffer;
      });
    function switchTpl(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-2019 (original work) Open Assessment Technologies SA ;
     */
    var states = {
      on: 'on',
      off: 'off'
    };
    var defaultConfig = {
      on: {
        label: __('On')
      },
      off: {
        label: __('Off'),
        active: true
      },
      title: ''
    };

    /**
     * The factory that creates a switch component
     *
     * @param {jQueryElement} $container - where to append the component
     * @param {Object} config - the component config
     * @param {Object} [config.on] - the on config
     * @param {String} [config.on.label] - the on button label
     * @param {Boolean} [config.on.active = false] - the default state
     * @param {Object} [config.off] - the off config
     * @param {String} [config.off.label] - the off button label
     * @param {Boolean} [config.off.active = true] - the default state
     * @param {String} [config.title] - the component title tooltip
     * @param {String} [config.name] - the component name (used by the element)
     * @param {Boolean} [config.monoStyle] - true if the styling should match for off & on
     * @returns {switchComponent} the component
     */
    function switchFactory($container, config) {
      var onElt;
      var offElt;

      /**
       * The component API
       */
      var api = {
        /**
         * Retrieve the component name
         * @returns {String} it's name
         */
        getName: function getName() {
          return this.config.name;
        },
        /**
         * Is the switch on ?
         * @returns {Boolean}
         */
        isOn: function isOn() {
          return this.is(states.on);
        },
        /**
         * Is the switch off ?
         * @returns {Boolean}
         */
        isOff: function isOff() {
          return !this.is(states.on);
        },
        /**
         * Switch On (if not yet on)
         * @returns {switchComponent}  chains
         * @fires switchComponent#change
         * @fires switchComponent#on
         */
        setOn: function setOn() {
          if (!this.isOn()) {
            this.setState(states.on, true);
          }
          if (this.is('rendered')) {
            offElt.removeClass('active');
            onElt.addClass('active');
            this.trigger('change', states.on).trigger(states.on);
          }
          return this;
        },
        /**
         * Switch Off (if not yet off)
         * @returns {switchComponent}  chains
         * @fires switchComponent#change
         * @fires switchComponent#off
         */
        setOff: function setOff() {
          if (!this.isOff()) {
            this.setState(states.on, false);
          }
          if (this.is('rendered')) {
            onElt.removeClass('active');
            offElt.addClass('active');
            this.trigger('change', states.off).trigger(states.off);
          }
          return this;
        },
        /**
         * Toggle on/off
         * @returns {switchComponent}  chains
         */
        toggle: function toggle() {
          return this.isOn() ? this.setOff() : this.setOn();
        },
        /**
         * Get the value
         * @returns {String} on/off
         */
        getValue: function getValue() {
          return this.is(states.on) ? states.on : states.off;
        }
      };
      var switchComponent = component(api, defaultConfig).setTemplate(switchTpl).on('init', function () {
        //generates a name if none
        if (!this.config.name) {
          this.config.name = 'switch-' + uuid();
        }

        //keeps defaults values if overridden
        this.config.on = _.defaults(this.config.on, defaultConfig.on);
        this.config.off = _.defaults(this.config.off, defaultConfig.off);

        //initial state
        if (this.config.on.active === true) {
          this.config.off.active = false;
          this.setOn();
        } else {
          this.setOff();
        }
        this.render($container);
      }).on('render', function () {
        var self = this;
        var $component = this.getElement();
        onElt = $$1('.' + states.on, $component);
        offElt = $$1('.' + states.off, $component);

        //switch
        $$1(':checkbox', $component).on('change', function (e) {
          e.preventDefault();
          self.toggle();
        });
      });
      _.defer(function () {
        switchComponent.init(config);
      });
      return switchComponent;
    }

    return switchFactory;

});
