"use strict";

exports.__esModule = true;
exports.default = void 0;

var _template = _interopRequireDefault(require("template7"));

var _utils = require("../../shared/utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _default = {
  name: 'routerTemplateLoader',
  proto: {
    templateLoader: function templateLoader(_temp) {
      var _ref = _temp === void 0 ? {} : _temp,
          template = _ref.template,
          templateUrl = _ref.templateUrl,
          options = _ref.options,
          resolve = _ref.resolve,
          reject = _ref.reject;

      var router = this;

      function compile(t) {
        var compiledHtml;
        var context;

        try {
          context = options.context || {};
          if (typeof context === 'function') context = context.call(router);else if (typeof context === 'string') {
            try {
              context = JSON.parse(context);
            } catch (err) {
              reject();
              throw err;
            }
          }

          if (typeof t === 'function') {
            compiledHtml = t(context);
          } else {
            compiledHtml = _template.default.compile(t)((0, _utils.extend)({}, context || {}, {
              $app: router.app,
              $root: (0, _utils.extend)({}, router.app.data, router.app.methods),
              $route: options.route,
              $f7route: options.route,
              $router: router,
              $f7router: router,
              $theme: {
                ios: router.app.theme === 'ios',
                md: router.app.theme === 'md',
                aurora: router.app.theme === 'aurora'
              }
            }));
          }
        } catch (err) {
          reject();
          throw err;
        }

        resolve(compiledHtml, {
          context: context
        });
      }

      if (templateUrl) {
        // Load via XHR
        if (router.xhrAbortController) {
          router.xhrAbortController.abort();
          router.xhrAbortController = false;
        }

        router.xhrRequest(templateUrl, options).then(function (templateContent) {
          compile(templateContent);
        }).catch(function () {
          reject();
        });
      } else {
        compile(template);
      }
    },
    modalTemplateLoader: function modalTemplateLoader(_temp2) {
      var _ref2 = _temp2 === void 0 ? {} : _temp2,
          template = _ref2.template,
          templateUrl = _ref2.templateUrl,
          options = _ref2.options,
          resolve = _ref2.resolve,
          reject = _ref2.reject;

      var router = this;
      return router.templateLoader({
        template: template,
        templateUrl: templateUrl,
        options: options,
        resolve: resolve,
        reject: reject
      });
    },
    tabTemplateLoader: function tabTemplateLoader(_temp3) {
      var _ref3 = _temp3 === void 0 ? {} : _temp3,
          template = _ref3.template,
          templateUrl = _ref3.templateUrl,
          options = _ref3.options,
          resolve = _ref3.resolve,
          reject = _ref3.reject;

      var router = this;
      return router.templateLoader({
        template: template,
        templateUrl: templateUrl,
        options: options,
        resolve: resolve,
        reject: reject
      });
    },
    pageTemplateLoader: function pageTemplateLoader(_temp4) {
      var _ref4 = _temp4 === void 0 ? {} : _temp4,
          template = _ref4.template,
          templateUrl = _ref4.templateUrl,
          options = _ref4.options,
          _resolve = _ref4.resolve,
          reject = _ref4.reject;

      var router = this;
      return router.templateLoader({
        template: template,
        templateUrl: templateUrl,
        options: options,
        resolve: function resolve(html, newOptions) {
          if (newOptions === void 0) {
            newOptions = {};
          }

          _resolve(router.getPageEl(html), newOptions);
        },
        reject: reject
      });
    }
  }
};
exports.default = _default;