import { reactive as W, computed as b, watch as R, onMounted as S, onUnmounted as V, toRefs as k, defineComponent as I, ref as P, openBlock as $, createElementBlock as E, normalizeClass as F, unref as L } from "vue";
import { u as B, E as w, b as H } from "./layer-select-Ba4IEiWY.js";
import { ViewEventType as x } from "@mapgis/webclient-common";
var f = /* @__PURE__ */ ((a) => (a.Meters = "m", a.Kilometers = "km", a.Miles = "mi", a.Feet = "ft", a.NauticalMiles = "nm", a))(f || {});
const K = {
  /**
   *@description 比例尺最大宽度(像素单位px)
   */
  maxWidth: {
    type: Number,
    default: 100
  },
  /**
   * @description 单位，imperial:英制,mi(英里),ft(英尺);metric:公制,km(千米),m(米);nautical:海里制,nm(海里)
   */
  unit: {
    type: String,
    default: "metric"
  }
}, T = {}, z = (a, v, p) => {
  const { viewState: i } = B(), o = W({
    maxWidth: a.maxWidth || 100,
    unit: a.unit || "metric"
  }), d = 63710088e-1, g = b(() => {
    let e;
    return i.engine === w.MapboxGL ? e = i.view.getInnerView().getContainer() : i.engine === w.Leaflet ? e = i.view.getInnerView().getContainer().parentElement : i.engine === w.Cesium && (e = i.view.getInnerView().container), e;
  });
  R(
    () => o.unit,
    () => {
      h();
    }
  ), S(() => {
    i.view.on(x.viewChange, h), h();
  }), V(() => {
    i.view.off(x.viewChange, h);
  });
  function h() {
    const e = o.maxWidth, n = g.value.clientHeight / 2, t = g.value.clientWidth / 2 - e / 2, r = { x: t, y: n }, c = { x: t + e, y: n }, l = i.view.toMap(r), u = i.view.toMap(c);
    let s;
    if (l && u ? s = N(
      { lng: l.coordinates[0], lat: l.coordinates[1] },
      { lng: u.coordinates[0], lat: u.coordinates[1] }
    ) : s = 2.6011608898428814e6, o.unit === "imperial") {
      const m = 3.2808 * s;
      if (m > 5280) {
        const y = m / 5280;
        M(e, y, f.Miles);
      } else
        M(e, m, f.Feet);
    } else if (o.unit === "nautical") {
      const m = s / 1852;
      M(e, m, f.NauticalMiles);
    } else s >= 1e3 ? M(e, s / 1e3, f.Kilometers) : M(e, s, f.Meters);
  }
  function M(e, n, t) {
    const r = C(n), c = r / n;
    p.value.style.width = `${e * c}px`, p.value.innerHTML = `${r}&nbsp;${t}`;
  }
  function _(e) {
    const n = Math.pow(10, Math.ceil(-Math.log(e) / Math.LN10));
    return Math.round(e * n) / n;
  }
  function C(e) {
    const n = Math.pow(10, `${Math.floor(e)}`.length - 1);
    let t = e / n;
    return t = t >= 10 ? 10 : t >= 5 ? 5 : t >= 3 ? 3 : t >= 2 ? 2 : t >= 1 ? 1 : _(t), n * t;
  }
  function N(e, n) {
    const { lat: t, lng: r } = e, c = Math.PI / 180, l = t * c, u = n.lat * c, s = Math.sin(l) * Math.sin(u) + Math.cos(l) * Math.cos(u) * Math.cos((n.lng - r) * c);
    return d * Math.acos(Math.min(s, 1));
  }
  return {
    ...k(o)
  };
}, A = /* @__PURE__ */ I({
  name: "MapgisScale",
  __name: "scale",
  props: K,
  emits: T,
  setup(a, { emit: v }) {
    const p = a, i = v, o = H("scale"), d = P();
    return z(p, i, d), (g, h) => ($(), E("div", {
      class: F([L(o).b()]),
      ref_key: "scaleRef",
      ref: d
    }, null, 2));
  }
});
export {
  f as S,
  A as _,
  K as a,
  T as s,
  z as u
};
