'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

require('../../../composables/index.js');
var usePosition = require('../../../composables/private/use-position.js');
var index$2 = require('../../ui/index.js');
var vm = require('../../../utils/private/vm.js');
var vue = require('vue');
var index$3 = require('../../viewer/index.js');
var render = require('../../../utils/private/render.js');
var emits = require('../../../utils/emits.js');
var cesiumHelpers = require('../../../utils/cesium-helpers.js');
var index = require('../../../composables/use-common/index.js');
var index$1 = require('../../../composables/use-locale/index.js');

const overviewProps = {
  position: {
    type: String,
    default: "bottom-right",
    validator: (v) => ["top-right", "top-left", "bottom-right", "bottom-left"].includes(v)
  },
  offset: {
    type: Array,
    validator: (v) => v.length === 2
  },
  width: {
    type: String,
    default: "150px"
  },
  height: {
    type: String,
    default: "150px"
  },
  border: {
    type: String,
    default: "solid 4px rgb(255, 255, 255)"
  },
  borderRadius: {
    type: String
  },
  toggleOpts: {
    type: Object
  },
  viewerOpts: {
    type: Object
  },
  centerRectColor: {
    type: [Object, Array, String],
    default: "#ff000080"
  },
  widthFactor: {
    type: Number,
    default: 2
  },
  heightFactor: {
    type: Number,
    default: 2
  },
  modelValue: {
    type: Boolean,
    default: true
  }
};
var OverviewMap = vue.defineComponent({
  name: "VcOverviewMap",
  props: overviewProps,
  emits: {
    ...emits.commonEmits,
    "update:modelValue": (value) => true
  },
  setup(props, ctx) {
    const instance = vue.getCurrentInstance();
    instance.cesiumClass = "VcOverviewMap";
    instance.cesiumEvents = [];
    const commonState = index["default"](props, ctx, instance);
    if (commonState === void 0) {
      return;
    }
    const { t } = index$1.useLocale();
    const { $services } = commonState;
    const rootRef = vue.ref(null);
    const toggleBtnRef = vue.ref(null);
    const rootStyle = vue.reactive({});
    const tooltipRef = vue.ref(null);
    const viewerRef = vue.ref(null);
    const positionState = usePosition["default"](props, $services);
    const showing = vue.ref(props.modelValue);
    let unwatchFns = [];
    let overviewViewer;
    let centerRect;
    const toggleOpts = vue.computed(() => {
      return Object.assign({}, {
        color: "#fff",
        background: "#3f4854",
        icon: "vc-icons-overview-toggle",
        size: "15px",
        tooltip: {
          delay: 500,
          anchor: "bottom middle",
          offset: [0, 20],
          tip: void 0
        }
      }, props.toggleOpts);
    });
    const viewerOpts = vue.computed(() => {
      return Object.assign({}, {
        removeCesiumScript: false,
        showCredit: false,
        sceneMode: 2,
        containerId: "vc-overview-map"
      }, props.viewerOpts);
    });
    instance.createCesiumObject = async () => {
      const { viewer } = $services;
      const viewerElement = viewer._element;
      viewerElement.appendChild(vm.$(rootRef));
      return [vm.$(rootRef), vm.$(viewerRef)];
    };
    instance.mount = async () => {
      updateRootStyle();
      const { viewer } = $services;
      viewer.clock.onTick.addEventListener(onClockTick);
      return true;
    };
    instance.unmount = async () => {
      const { viewer } = $services;
      const viewerElement = viewer._element;
      viewer.clock.onTick.removeEventListener(onClockTick);
      viewerElement.contains(vm.$(rootRef)) && viewerElement.removeChild(vm.$(rootRef));
      return true;
    };
    const onClockTick = () => {
      if (overviewViewer) {
        const { viewer: parentViewer } = $services;
        const parentCameraRectangle = parentViewer.camera.computeViewRectangle();
        const { defined } = Cesium;
        if (!defined(parentCameraRectangle)) {
          return;
        }
        const rectangle = parentCameraRectangle.expand(props.widthFactor, props.heightFactor);
        overviewViewer.camera.flyTo({
          destination: rectangle.clone(),
          orientation: {
            heading: parentViewer.camera.heading,
            pitch: parentViewer.camera.pitch,
            roll: parentViewer.camera.roll
          },
          duration: 0
        });
        const { Cartesian3, SceneTransforms } = Cesium;
        const wnPosition = Cartesian3.fromRadians(parentCameraRectangle.west, parentCameraRectangle.north);
        const enPosition = Cartesian3.fromRadians(parentCameraRectangle.east, parentCameraRectangle.north);
        const wsPosition = Cartesian3.fromRadians(parentCameraRectangle.west, parentCameraRectangle.south);
        const esPosition = Cartesian3.fromRadians(parentCameraRectangle.east, parentCameraRectangle.south);
        const scene = overviewViewer.scene;
        const wnWindowPosition = SceneTransforms.wgs84ToWindowCoordinates(scene, wnPosition);
        const enWindowPosition = SceneTransforms.wgs84ToWindowCoordinates(scene, enPosition);
        const wsWindowPosition = SceneTransforms.wgs84ToWindowCoordinates(scene, wsPosition);
        const esWindowPosition = SceneTransforms.wgs84ToWindowCoordinates(scene, esPosition);
        if (!defined(wnWindowPosition) || !defined(enWindowPosition) || !defined(wsWindowPosition) || !defined(esWindowPosition)) {
          return;
        }
        const width = enWindowPosition.x - wnWindowPosition.x;
        const height = wsWindowPosition.y - wnWindowPosition.y;
        const x = (wnWindowPosition.x + enWindowPosition.x) / 2 - width / 2;
        const y = (wnWindowPosition.y + wsWindowPosition.y) / 2 - height / 2;
        if (width <= 0 || height <= 0) {
          return;
        }
        const boundingRectangle = new Cesium.BoundingRectangle(x, y, width, height);
        centerRect.rectangle = boundingRectangle;
        centerRect.material.uniforms.color = cesiumHelpers.makeColor(props.centerRectColor);
        centerRect.show = true;
      }
    };
    const onViewerReady = (readyObj) => {
      const { viewer } = readyObj;
      overviewViewer = viewer;
      const control = viewer.scene.screenSpaceCameraController;
      control.enableRotate = false;
      control.enableTranslate = false;
      control.enableZoom = false;
      control.enableTilt = false;
      control.enableLook = false;
      overviewViewer.scene.highDynamicRange = false;
      overviewViewer.scene.globe.enableLighting = false;
      overviewViewer.scene.globe.showWaterEffect = false;
      overviewViewer.scene.globe.depthTestAgainstTerrain = false;
      overviewViewer.scene.skyAtmosphere.show = false;
      overviewViewer.scene.fog.enabled = false;
      overviewViewer.scene.skyBox.show = false;
      overviewViewer.scene.sun.show = false;
      overviewViewer.scene.moon.show = false;
      overviewViewer.scene.highDynamicRange = false;
      overviewViewer.scene.globe.showGroundAtmosphere = false;
      centerRect = new Cesium.ViewportQuad(new Cesium.BoundingRectangle(150, 100, 100, 50));
      centerRect.show = false;
      overviewViewer.scene.primitives.add(centerRect);
    };
    const updateRootStyle = () => {
      var _a;
      const css = positionState.style.value;
      rootStyle.left = css.left;
      rootStyle.top = css.top;
      rootStyle.transform = css.transform;
      rootStyle["pointer-events"] = "none";
      css.borderRadius = props.borderRadius;
      css.border = props.border;
      if (showing.value) {
        css.width = props.width;
        css.height = props.height;
      } else {
        const reg = /(\d+)/g;
        const regResult = reg.exec(props.border);
        const boder = (regResult == null ? void 0 : regResult.length) ? parseFloat(regResult[0]) : 0;
        const toggleBtnRefStyle = getComputedStyle((_a = vm.$(toggleBtnRef)) == null ? void 0 : _a.$el);
        css.width = `${parseFloat(toggleBtnRefStyle.width) + parseFloat(toggleBtnRefStyle.padding) + boder}px`;
        css.height = `${parseFloat(toggleBtnRefStyle.height) + parseFloat(toggleBtnRefStyle.padding) + boder}px`;
      }
      Object.assign(rootStyle, css);
    };
    const onToggle = () => {
      if (showing.value) {
        minimize();
      } else {
        restore();
      }
      showing.value = !showing.value;
      ctx.emit("update:modelValue", showing.value);
    };
    const minimize = () => {
      var _a;
      const reg = /(\d+)/g;
      const regResult = reg.exec(props.border);
      const boder = (regResult == null ? void 0 : regResult.length) ? parseFloat(regResult[0]) : 0;
      const toggleBtnRefStyle = getComputedStyle((_a = vm.$(toggleBtnRef)) == null ? void 0 : _a.$el);
      rootStyle.width = `${parseFloat(toggleBtnRefStyle.width) + parseFloat(toggleBtnRefStyle.padding) + boder}px`;
      rootStyle.height = `${parseFloat(toggleBtnRefStyle.height) + parseFloat(toggleBtnRefStyle.padding) + boder}px`;
    };
    const restore = () => {
      rootStyle.width = props.width;
      rootStyle.height = props.height;
    };
    vue.onUnmounted(() => {
      unwatchFns.forEach((item) => item());
      unwatchFns = [];
    });
    return () => {
      const children = [];
      children.push(vue.h(index$2.VcBtn, {
        ref: toggleBtnRef,
        class: "toggle toggle-" + props.position + (!showing.value ? " minimized " : ""),
        flat: true,
        dense: true,
        icon: toggleOpts.value.icon,
        size: toggleOpts.value.size,
        style: { color: toggleOpts.value.color, background: toggleOpts.value.background, "pointer-events": "auto" },
        onClick: onToggle
      }, () => toggleOpts.value.tooltip ? vue.h(index$2.VcTooltip, {
        ref: tooltipRef,
        ...toggleOpts.value.tooltip
      }, () => vue.h("strong", {}, toggleOpts.value.tooltip.tip || t(`vc.overview.${!showing.value ? "show" : "hidden"}`))) : vue.createCommentVNode("v-if")));
      children.push(vue.h(index$3["default"], {
        ref: viewerRef,
        ...viewerOpts.value,
        onReady: onViewerReady
      }, () => render.hSlot(ctx.slots.default)));
      return vue.h("div", {
        ref: rootRef,
        class: "vc-overview-map " + positionState.classes.value,
        style: rootStyle
      }, children);
    };
  }
});

exports["default"] = OverviewMap;
exports.overviewProps = overviewProps;
//# sourceMappingURL=index.js.map
