/**
 * vue-cesium - https://github.com/zouyaoji/vue-cesium
 * Vue 2.x components for CesiumJS.
 *
 * @package VueCesium
 * @author zouyaoji <370681295@qq.com>
 * @version 2.3.12
 * @license MIT
 * @homepage https://zouyaoji.top/vue-cesium
 * @copyright (c) 2018-2022, zouyaoji <370681295@qq.com>
 */
import _asyncToGenerator from '@babel/runtime-corejs2/helpers/esm/asyncToGenerator';
import _regeneratorRuntime from '@babel/runtime-corejs2/regenerator';
import _Promise from '@babel/runtime-corejs2/core-js/promise';
import mixinTerrainProvider from '../../mixins/providers/mixinTerrainProvider';
import bindEvents from '../../utils/bindEvent';
import { Events } from '../../utils/events';

var script = {
  name: 'VcProviderTerrainTianditu',
  mixins: [mixinTerrainProvider],
  props: {
    url: {
      type: String,
      default: 'https://{s}.tianditu.gov.cn/'
    },
    subdomains: {
      type: Array,
      default: function _default() {
        return ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'];
      }
    },
    pluginPath: {
      type: String,
      default: 'https://api.tianditu.gov.cn/cdn/plugins/cesium/cesiumTdt.js'
    },
    dataType: {
      type: String,
      default: 'int',
      validator: function validator(v) {
        return ['int', 'float'].includes(v);
      }
    },
    tileType: {
      type: String,
      default: 'heightmap',
      validator: function validator(v) {
        return ['heightmap', 'quantized-mesh'].includes(v);
      }
    },
    token: String
  },
  methods: {
    /**
     * 重写 createCesiumObject 方法。
     */
    createCesiumObject: function createCesiumObject() {
      var _this = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var that, unwatchFns, setPropWatchers, subdomains, pluginPath, token;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                that = _this;
                unwatchFns = _this.unwatchFns, setPropWatchers = _this.setPropWatchers, subdomains = _this.subdomains, pluginPath = _this.pluginPath, token = _this.token;
                return _context.abrupt("return", new _Promise(function (resolve, reject) {
                  that.$script = document.createElement('script');
                  document.body.appendChild(that.$script);
                  that.$script.src = pluginPath;

                  that.$script.onload = function () {
                    if (unwatchFns.length === 0) {
                      setPropWatchers(true);
                    }

                    var terrainUrls = [];

                    for (var i = 0; i < subdomains.length; i++) {
                      var url = _this.url.replace('{s}', subdomains[i]) + 'mapservice/swdx?tk=' + token;
                      terrainUrls.push(url);
                    }

                    resolve(new Cesium.GeoTerrainProvider({
                      urls: terrainUrls
                    }));
                  };
                }));

              case 3:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    unmount: function unmount() {
      var _this2 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
        var _this2$$script;

        var viewer, terrainProvider;
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                viewer = _this2.viewer, terrainProvider = _this2.terrainProvider;
                bindEvents.call(_this2, terrainProvider, Events['imagery-layer-events'], false);
                viewer.terrainProvider = new Cesium.EllipsoidTerrainProvider();
                (_this2$$script = _this2.$script) === null || _this2$$script === void 0 ? void 0 : _this2$$script.parentNode.removeChild(_this2.$script);
                return _context2.abrupt("return", true);

              case 5:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    }
  }
};

/* script */
var __vue_script__ = script;
/* template */

/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* functional template */

var __vue_is_functional_template__ = undefined;
/* component normalizer */

function __vue_normalize__(template, style, script, scope, functional, moduleIdentifier, createInjector, createInjectorSSR) {
  var component = (typeof script === 'function' ? script.options : script) || {}; // For security concerns, we use only base name in production mode.

  component.__file = "TiandituTerrainProvider.vue";

  if (!component.render) {
    component.render = template.render;
    component.staticRenderFns = template.staticRenderFns;
    component._compiled = true;
    if (functional) component.functional = true;
  }

  component._scopeId = scope;

  return component;
}
/* style inject */

/* style inject SSR */


var TiandituTerrainProvider = __vue_normalize__({}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__);

/*
 * @Author: zouyaoji@https://github.com/zouyaoji
 * @Date: 2022-04-26 11:05:59
 * @LastEditTime: 2022-04-26 11:05:59
 * @LastEditors: zouyaoji
 * @Description:
 * @FilePath: \vue-cesium-v2\src\components\terrain\tianditu\index.js
 */

function plugin(Vue) {

  if (plugin.installed) {
    return;
  }

  plugin.installed = true;
  Vue.component(TiandituTerrainProvider.name, TiandituTerrainProvider);
}

export default plugin;
export { TiandituTerrainProvider, plugin as install };
