import { defineComponent, getCurrentInstance, createCommentVNode } from 'vue';
import '../../../composables/index.mjs';
import { kebabCase } from '../../../utils/util.mjs';
import { getInstanceListener } from '../../../utils/private/vm.mjs';
import useProviders from '../../../composables/use-providers/index.mjs';
import useVueCesium from '../../../composables/use-vue-cesium/index.mjs';

var ProviderTerrainTianditu = defineComponent({
  name: "VcProviderTerrainTianditu",
  props: {
    url: {
      type: String,
      default: "https://{s}.tianditu.gov.cn/"
    },
    subdomains: {
      type: Array,
      default: () => ["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: (v) => ["int", "float"].includes(v)
    },
    tileType: {
      type: String,
      default: "heightmap",
      validator: (v) => ["heightmap", "quantized-mesh"].includes(v)
    },
    token: String
  },
  emits: ["beforeLoad", "ready", "destroyed", "readyPromise"],
  setup(props, ctx) {
    const instance = getCurrentInstance();
    instance.cesiumClass = "GeoTerrainProvider";
    const providersState = useProviders(props, ctx, instance);
    if (providersState === void 0) {
      return;
    }
    const vc = useVueCesium();
    let $script;
    instance.createCesiumObject = async () => {
      return new Promise((resolve, reject) => {
        $script = document.createElement("script");
        document.body.appendChild($script);
        $script.src = props.pluginPath;
        $script.onload = () => {
          if (providersState.unwatchFns.length === 0) {
            providersState.setPropsWatcher(true);
          }
          const terrainUrls = [];
          for (let i = 0; i < props.subdomains.length; i++) {
            const url = props.url.replace("{s}", props.subdomains[i]) + "mapservice/swdx?tk=" + props.token;
            terrainUrls.push(url);
          }
          resolve(new Cesium.GeoTerrainProvider({
            urls: terrainUrls
          }));
        };
      });
    };
    instance.unmount = async () => {
      const terrainProvider = new Cesium.EllipsoidTerrainProvider();
      terrainProvider.readyPromise.then(() => {
        const listener = getInstanceListener(instance, "readyPromise");
        listener && ctx.emit("readyPromise", terrainProvider, vc == null ? void 0 : vc.viewer, instance.proxy);
      });
      vc && (vc.viewer.terrainProvider = terrainProvider);
      $script == null ? void 0 : $script.parentNode.removeChild($script);
      return true;
    };
    return () => {
      var _a;
      return createCommentVNode(kebabCase(((_a = instance.proxy) == null ? void 0 : _a.$options.name) || ""));
    };
  }
});

export { ProviderTerrainTianditu as default };
//# sourceMappingURL=terrain.mjs.map
