define(['jquery', 'lodash', 'handlebars', 'lib/handlebars/helpers'], function ($$1, _, Handlebars, Helpers0) { 'use strict';

    $$1 = $$1 && Object.prototype.hasOwnProperty.call($$1, 'default') ? $$1['default'] : $$1;
    _ = _ && Object.prototype.hasOwnProperty.call(_, 'default') ? _['default'] : _;
    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 = "", helper, options, helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression;


      buffer += "<div class=\"grid-row clearfix pagination\">\n    <ul class=\"pages\">\n        <li class=\"first-page\"><span class=\"icon-fast-backward\" title=\""
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "First page", options) : helperMissing.call(depth0, "__", "First page", options)))
        + "\"></span></li>\n        <li class=\"previous\"><span class=\"icon-backward\" title=\""
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Previous", options) : helperMissing.call(depth0, "__", "Previous", options)))
        + "\"></span></li>\n        <li class=\"next\"><span class=\"icon-forward\" title=\""
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Next", options) : helperMissing.call(depth0, "__", "Next", options)))
        + "\"></span></li>\n        <li class=\"last-page\"><span class=\"icon-fast-forward\" title=\""
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Last page", options) : helperMissing.call(depth0, "__", "Last page", options)))
        + "\"></span></li>\n    </ul>\n</div>\n";
      return buffer;
      });
    function tpl(data, options, asString) {
      var html = Template(data, options);
      return (asString || true) ? html : $(html);
    }

    if (!Helpers0.__initialized) {
      Helpers0(Handlebars);
      Helpers0.__initialized = true;
    }
    var Template$1 = 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 += "<li class=\"page\"><span>";
      if (helper = helpers.page) { stack1 = helper.call(depth0, {hash:{},data:data}); }
      else { helper = (depth0 && depth0.page); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
      buffer += escapeExpression(stack1)
        + "</span></li>\n";
      return buffer;
      });
    function pageTpl(data, options, asString) {
      var html = Template$1(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) 2016  (original work) Open Assessment Technologies SA;
     *
     * @author Alexander Zagovorichev <zagovorichev@1pt.com>
     */
    var pages = {
      init: function () {
        var $paginationTpl;
        var generatePage = function generatePage(page) {
          return $$1(pageTpl({
            page: page
          }));
        };
        var separator = function separator() {
          var $page = generatePage('...');
          $page.addClass('separator');
          return $page;
        };
        var generatePart = function generatePart(from, to, activePage) {
          var i,
            pages = [],
            $page;
          for (i = from; i <= to; i++) {
            $page = generatePage(i);
            if (i === activePage) {
              $page.addClass('active');
            }
            pages.push($page);
          }
          return pages;
        };
        var generatePages = function generatePages(page, total) {
          var pages = [];
          if (total <= 7) {
            pages = pages.concat(generatePart(1, total, page));
          } else {
            if (page < 5) {
              pages = pages.concat(generatePart(1, 5, page));
              pages = pages.concat(separator());
              pages = pages.concat(generatePart(total, total, page));
            } else {
              if (page >= total - 3) {
                pages = pages.concat(generatePart(1, 1, page));
                pages = pages.concat(separator());
                pages = pages.concat(generatePart(total - 3, total, page));
              } else {
                pages = pages.concat(generatePart(1, 1, page));
                pages = pages.concat(separator());
                pages = pages.concat(generatePart(page - 1, page + 1, page));
                pages = pages.concat(separator());
                pages = pages.concat(generatePart(total, total, page));
              }
            }
          }
          return pages;
        };
        var dropPages = function dropPages() {
          $$1('.page', $paginationTpl).remove();
        };
        var getForwardBtn = function () {
          return $$1('.next', $paginationTpl);
        };
        var bindPages = function bindPages(list) {
          var $point = getForwardBtn();
          _.each(list, function ($page) {
            $page.insertBefore($point);
          });
        };
        var pagination = {
          render: function render($container) {
            $paginationTpl = $$1(tpl());
            $container.append($paginationTpl);
          },
          forwardButton: function forwardButton() {
            return getForwardBtn();
          },
          backwardButton: function backwardButton() {
            return $$1('.previous', $paginationTpl);
          },
          pageButtons: function pageButton() {
            return $$1('.page', $paginationTpl);
          },
          firstPageButton: function lastPageButton() {
            return $$1('.first-page', $paginationTpl);
          },
          lastPageButton: function lastPageButton() {
            return $$1('.last-page', $paginationTpl);
          },
          setPages: function setPages(page, total) {
            var pages = generatePages(page, total);
            dropPages();
            bindPages(pages);
          },
          disableButton: function disableButton($btn) {
            if (!$btn.hasClass('disabled')) {
              $btn.addClass('disabled');
            }
          },
          enableButton: function enableButton($btn) {
            if ($btn.hasClass('disabled')) {
              $btn.removeClass('disabled');
            }
          },
          destroy: function destroy() {
            $paginationTpl.remove();
          },
          disable: function disable() {
            var self = this;
            this.disableButton(this.backwardButton());
            this.disableButton(this.firstPageButton());
            $$1('.page', $paginationTpl).each(function () {
              self.disableButton($$1(this));
            });
            this.disableButton(this.lastPageButton());
            this.disableButton(this.forwardButton());
          },
          enable: function enable() {
            var self = this;
            // restore buttons
            $$1('.page', $paginationTpl).each(function () {
              self.enableButton($$1(this));
            });
          }
        };
        return pagination;
      }
    };

    return pages;

});
