define(['jquery', 'lodash', 'core/pluginifier', 'ui/resourcemgr/fileBrowser', 'ui/resourcemgr/filePreview', 'ui/resourcemgr/fileSelector', 'handlebars', 'lib/handlebars/helpers', 'ui/modal', 'css!ui/resourcemgr/css/resourcemgr.css'], function ($$1, _, Pluginifier, fileBrowser, filePreview, fileSelector, Handlebars, Helpers0, modal, resourcemgr_css) { 'use strict';

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

    function program1(depth0,data) {
      
      var stack1, helper;
      if (helper = helpers.className) { stack1 = helper.call(depth0, {hash:{},data:data}); }
      else { helper = (depth0 && depth0.className); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
      return escapeExpression(stack1);
      }

      buffer += "<div class=\"resourcemgr modal ";
      stack1 = helpers['if'].call(depth0, (depth0 && depth0.className), {hash:{},inverse:self.noop,fn:self.program(1, program1, data),data:data});
      if(stack1 || stack1 === 0) { buffer += stack1; }
      buffer += "\">\n\n    <h2>";
      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)
        + "</h2>\n\n    <div class=\"file-wrapper\">\n\n        <!-- left section: items selection -->\n        <section class=\"file-browser\">\n            <h1>"
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Browse resources", options) : helperMissing.call(depth0, "__", "Browse resources", options)))
        + "</h1>\n            <div class=\"file-browser-wrapper\"></div>\n        </section>\n\n        <!-- test editor  -->\n        <section class=\"file-selector\">\n\n            <h1>\n                <div class=\"title lft\"></div>\n                <div class=\"upload-switcher rgt\">\n                    <a href=\"#\" class=\"btn-info small upload hidden\"><span class=\"icon-add\"></span>"
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Add file(s)", options) : helperMissing.call(depth0, "__", "Add file(s)", options)))
        + "</a>\n                    <a href=\"#\" class=\"btn-info small listing\"><span class=\"icon-undo\"></span>"
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Back to listing", options) : helperMissing.call(depth0, "__", "Back to listing", options)))
        + "</a>\n                </div>\n            </h1>\n\n            <div class=\"empty\">\n                "
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "No files", options) : helperMissing.call(depth0, "__", "No files", options)))
        + "\n            </div>\n\n            <ul class=\"files\"></ul>\n\n            <div class=\"file-upload-container\"></div>\n\n            <div class=\"pagination-bottom\"></div>\n\n        </section>\n\n        <section class=\"file-preview\">\n\n            <h1>"
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Preview", options) : helperMissing.call(depth0, "__", "Preview", options)))
        + "</h1>\n\n            <div class=\"previewer\">\n                <p class=\"nopreview\"></p>\n            </div>\n\n            <h2 class=\"toggler\" data-toggle=\"~ .file-properties\">"
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "File Properties", options) : helperMissing.call(depth0, "__", "File Properties", options)))
        + "</h2>\n\n            <div class=\"file-properties\">\n\n                <div class=\"grid-row\">\n                    <div class=\"col-2\">\n                        "
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Type", options) : helperMissing.call(depth0, "__", "Type", options)))
        + "\n                    </div>\n                    <div class=\"col-10 prop-type\"></div>\n                </div>\n\n                <div class=\"grid-row\">\n                    <div class=\"col-2\">\n                        "
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Size", options) : helperMissing.call(depth0, "__", "Size", options)))
        + "\n                    </div>\n                    <div class=\"col-10 prop-size\"></div>\n                </div>\n\n                <div class=\"grid-row prop-url\">\n                    <div class=\"actions\">\n                        <a href=\"#\" download=\"\" target=\"_blank\" class=\"tlb-button-off download hidden\" title=\""
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Download this file", options) : helperMissing.call(depth0, "__", "Download this file", options)))
        + "\">\n                            <button class=\"btn-info small\">\n                                <span class=\"icon-download\"></span>"
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Download this file", options) : helperMissing.call(depth0, "__", "Download this file", options)))
        + "\n                            </button>\n                        </a>\n                    </div>\n                </div>\n            </div>\n\n            <h2 class=\"toggler\" data-toggle=\"~ .actions\">"
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Actions", options) : helperMissing.call(depth0, "__", "Actions", options)))
        + "</h2>\n\n            <div class=\"actions\">\n                <button class=\"btn-success select-action small\" disabled>\n                    <span class=\"icon-move-item\"></span>"
        + escapeExpression((helper = helpers.__ || (depth0 && depth0.__),options={hash:{},data:data},helper ? helper.call(depth0, "Select", options) : helperMissing.call(depth0, "__", "Select", options)))
        + "\n                </button>\n            </div>\n\n        </section>\n\n    </div>\n</div>\n";
      return buffer;
      });
    function layout(data, options, asString) {
      var html = Template(data, options);
      return (asString || true) ? html : $(html);
    }

    var ns = 'resourcemgr';
    var dataNs = 'ui.' + ns;
    var defaults = {
      mediaSources: [{
        root: 'local',
        path: '/'
      }],
      open: true,
      appendContainer: '.tao-scope:first',
      title: ''
    };

    /**
     * The ResourceMgr component helps you to browse and select external resources.
     * @exports ui/resourcemgr
     */
    var resourceMgr = {
      /**
       * Initialize the plugin.
       *
       * Called the jQuery way once registered by the Pluginifier.
       * @example $('selector').resourcemgr({
       *
       *  });
       *
       * @constructor
       * @param {Object} options - the plugin options
       * @param {Sring|Boolean} [options.bindEvent = 'click'] - the event that trigger the toggling
       * @param {String} options.url - the URL of the service used to retrieve the resources.
       * @fires ResourceMgr#create.resourcemgr
       * @returns {jQueryElement} for chaining
       */
      init: function (options) {
        var that = resourceMgr;

        //get options using default
        options = _.defaults(options, defaults);
        return this.each(function () {
          var $elt = $$1(this);
          var $target;
          if (!$elt.data(dataNs)) {
            //add data to the element
            $elt.data(dataNs, options);

            //auto bind events configured in options
            _.functions(options).forEach(function (eventName) {
              $elt.on(eventName + '.' + ns, function () {
                options[eventName].apply($elt, arguments);
              });
            });
            $target = options.$target || that._createTarget($elt);
            $target.modal({
              startClosed: true,
              minWidth: 'responsive'
            });

            //rethrow some events
            $target.on('select.' + ns, function (e, files) {
              that._close($elt);
              $elt.trigger(e, [files]);
            });
            $target.on('closed.modal', function () {
              $elt.trigger('close.' + ns);
            });
            //initialize the components
            const $fileBrowser = $$1('.file-browser .file-browser-wrapper', $target);
            if (options.mediaSourcesUrl) {
              $$1.getJSON(options.mediaSourcesUrl).done(function (data) {
                const mediaSources = data || defaults.mediaSources;
                for (let i = 0; i < mediaSources.length; i++) {
                  options.root = mediaSources[i].root;
                  options.path = mediaSources[i].path;
                  $fileBrowser.append('<div class="' + options.root + '"><ul class="folders"></ul></div>');
                  fileBrowser(options);
                }
              }).fail(function () {
                for (let i = 0; i < defaults.mediaSources.length; i++) {
                  options.root = defaults.mediaSources[i].root;
                  options.path = defaults.mediaSources[i].path;
                  $fileBrowser.append('<div class="' + options.root + '"><ul class="folders"></ul></div>');
                  fileBrowser(options);
                }
              });
            } else if (options.path && options.root) {
              $fileBrowser.append('<div class="' + options.root + '"><ul class="folders"></ul></div>');
              fileBrowser(options);
            }
            $fileBrowser.find('li.root:last').addClass('active');
            fileSelector(options);
            filePreview(options);

            /**
             * The plugin have been created.
             * @event ResourceMgr#create.resourcemgr
             */
            $elt.trigger('create.' + ns, [$target[0]]);
            if (options.open) {
              that._open($elt);
            }
          } else {
            options = $elt.data(dataNs);
            if (options.open) {
              that._open($elt);
            }
          }
        });
      },
      _createTarget: function ($elt) {
        var options = $elt.data(dataNs);
        if (options) {
          //create an identifier to the target content
          options.targetId = 'resourcemgr-' + $$1(document).find('.resourcemgr').length;

          //generate
          options.$target = $$1(layout({
            title: options.title || '',
            className: options.className || ''
          }));
          options.$target.attr('id', options.targetId).css('display', 'none').appendTo(options.appendContainer);
          $elt.data(dataNs, options);
        }
        return options.$target;
      },
      _open: function ($elt) {
        var options = $elt.data(dataNs);
        if (options && options.$target) {
          options.$target.modal('open');

          /**
           * Open the resource manager.
           * @event ResourceMgr#open.resourcemgr
           */
          $elt.trigger('open.' + ns);
        }
      },
      _close: function ($elt) {
        var options = $elt.data(dataNs);
        if (options && options.$target) {
          options.$target.modal('close');
        }
      },
      /**
       * Destroy completely the plugin.
       *
       * Called the jQuery way once registered by the Pluginifier.
       * @example $('selector').resourcemgr('destroy');
       * @public
       */
      destroy: function () {
        this.each(function () {
          var $elt = $$1(this);
          var options = $elt.data(dataNs);
          $elt.data(dataNs, null);
          /*eslint no-undefined: "error"*/
          if (typeof options.bindEvent !== 'undefined' && options.bindEvent !== false) {
            $elt.off(options.bindEvent);
          }
          if (options.targetId) {
            options.$target.on('closed.modal', function () {
              $$1('#' + options.targetId).remove();
              $$1(window).off('resize.resourcemgr');
              /**
               * The plugin have been destroyed.
               * @event ResourceMgr#destroy.resourcemgr
               */
              $elt.trigger('destroy.' + ns);
            }).modal('close');
          } else {
            $$1(window).off('resize.resourcemgr');
            /**
             * The plugin have been destroyed.
             * @event ResourceMgr#destroy.resourcemgr
             */
            $elt.trigger('destroy.' + ns);
          }
        });
      }
    };

    //Register the resourcemgr to behave as a jQuery plugin.
    Pluginifier.register(ns, resourceMgr);

});
