define(['lodash', 'ui/component', 'ui/taskQueueButton/taskable', 'handlebars', 'lib/handlebars/helpers', 'css!ui/taskQueueButton/css/treeButton.css'], function (_, component, makeTaskable, Handlebars, Helpers0, treeButton_css) { 'use strict';

    _ = _ && Object.prototype.hasOwnProperty.call(_, 'default') ? _['default'] : _;
    component = component && Object.prototype.hasOwnProperty.call(component, 'default') ? component['default'] : component;
    makeTaskable = makeTaskable && Object.prototype.hasOwnProperty.call(makeTaskable, 'default') ? makeTaskable['default'] : makeTaskable;
    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, functionType="function", escapeExpression=this.escapeExpression;


      buffer += "<div class=\"tree-button-component\">\n    <a class=\"li-inner\">\n        <span class=\"spinner-icon icon-loop spinning\"></span>\n        <span class=\"glyph start-icon icon-";
      if (helper = helpers.icon) { stack1 = helper.call(depth0, {hash:{},data:data}); }
      else { helper = (depth0 && depth0.icon); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
      buffer += escapeExpression(stack1)
        + "\"></span>\n        <span class=\"action-name\">";
      if (helper = helpers.label) { stack1 = helper.call(depth0, {hash:{},data:data}); }
      else { helper = (depth0 && depth0.label); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
      buffer += escapeExpression(stack1)
        + "</span>\n    </a>\n</div>";
      return buffer;
      });
    function buttonTpl(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 ;
     */
    var _defaults = {
      icon: 'property-advanced',
      label: 'OK'
    };
    var buttonApi = {
      /**
       * Start the button spinning
       * @returns {treeTaskButton}
       */
      start: function start() {
        this.createTask();
        this.setState('started', true);
        this.trigger('start');
        return this;
      },
      /**
       * Stop the button spinning
       * @returns {treeTaskButton}
       */
      stop: function stop() {
        if (this.is('started')) {
          this.setState('started', false);
          this.trigger('stop');
        }
        return this;
      }
    };

    /**
     * Create a button that will trigger a task creation when clicked
     *
     * @param {Object} config - the component config
     * @param {String} config.icon - the button icon
     * @param {String} config.label - the button's label
     * @param {Object} config.taskQueue - the task queue model to be used
     * @param {String} config.taskCreationUrl - endpoint to create a task
     * @param {Object|Function} [config.taskCreationData] - the parameters that will be send to the task creation request
     * @param {JQuery} [config.taskReportContainer] - the container where the inline report can be printed to
     * @return {treeTaskButton} the component
     *
     * @event start - When the button starts spinning
     * @event stop - When the button stops spinning
     * @event finished - When task is finished within the polling duration allowed by the task queue model
     * @event enqueued - when task has not time to finish within the polling duration allowed by the task queue model
     */
    function treeTaskButtonFactory(config) {
      var initConfig = _.defaults(config || {}, _defaults);

      /**
       * @typedef {treeTaskButton} the component
       */
      return makeTaskable(component(buttonApi)).on('finished', function () {
        this.stop();
      }).on('enqueued', function () {
        this.stop();
      }).setTemplate(buttonTpl).init(initConfig);
    }

    return treeTaskButtonFactory;

});
