/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as h } from "../../chunks/runtime.js";
import { u as o } from "../../chunks/useT9n.js";
import { css as u, html as s } from "lit";
import { createRef as c, ref as d } from "lit-html/directives/ref.js";
import { LitElement as m, createEvent as n } from "@arcgis/lumina";
import * as i from "@arcgis/core/geometry/operators/geodesicBufferOperator.js";
import * as r from "@arcgis/core/geometry/operators/bufferOperator.js";
const f = u`:host{display:block}.c-container{display:inline-flex}.flex-1{flex:"1"}.padding-end-1{padding-inline-end:1rem}.w-50{width:50%}`;
class p extends m {
  constructor() {
    super(...arguments), this.appearance = "text", this.distance = 0, this.geometries = [], this.max = 0, this.min = 0, this.sliderTicks = 10, this.unionResults = !0, this.unit = "meters", this.disabled = !1, this._translations = o({ name: "buffer-tools", blocking: !0 }), this._unitElement = c(), this.bufferComplete = n(), this.distanceChanged = n(), this.unitChanged = n();
  }
  static {
    this.properties = { appearance: 1, distance: 9, geometries: 0, max: 9, min: 9, sliderTicks: 9, unionResults: 5, unit: 1, disabled: 5, _translations: 16, _units: 16 };
  }
  static {
    this.styles = f;
  }
  willUpdate(t) {
    t.has("geometries") && (this.hasUpdated || this.geometries?.length > 0) && this.geometriesWatchHandler(), t.has("disabled") && (this.hasUpdated || this.disabled !== !1) && this.disabledWatchHandler(), t.has("_translations") && this._defineTranslations(), t.has("unit") && this.hasUpdated && this._setUnit(this.unit), t.has("distance") && this.hasUpdated && this._setDistance({ target: { value: this.distance } });
  }
  geometriesWatchHandler() {
    this._buffer();
  }
  disabledWatchHandler() {
    this._buffer();
  }
  async getTranslatedUnit(t) {
    return this._units || await this._defineTranslations(), this._units ? this._units[t] : "";
  }
  render() {
    return this.appearance === "text" ? this._getTextBoxDisplay() : this._getSliderDisplay();
  }
  async _defineTranslations() {
    this._units = {
      feet: this._translations.feet || "Feet",
      meters: this._translations.meters || "Meters",
      miles: this._translations.miles || "Miles",
      kilometers: this._translations.kilometers || "Kilometers"
    };
  }
  _getUnits() {
    return this._units ? Object.keys(this._units).map((t) => this._units ? s`<calcite-option .label=${this._units[t]} .selected=${this.unit === t} .value=${t}></calcite-option>` : void 0) : void 0;
  }
  _setDistance(t) {
    const e = Number.parseInt(t.target.value, 10);
    this.distance !== e && e >= this.min && (this.distanceChanged.emit({
      oldValue: this.distance,
      newValue: e
    }), this.distance = e, this.distance > 0 ? this._buffer() : this.bufferComplete.emit(void 0));
  }
  _setUnit(t) {
    this.unitChanged.emit({
      oldValue: this.unit,
      newValue: t
    }), this.unit = t, this._buffer();
  }
  _buffer() {
    this.disabled ? this.bufferComplete.emit(void 0) : (this._bufferTimeout && clearTimeout(this._bufferTimeout), this._bufferTimeout = setTimeout(async () => {
      if (this.geometries?.length > 0 && this.unit && this.distance > 0 && i && r) {
        i.isLoaded() || await i.load();
        const e = this.geometries[0].spatialReference, a = this.geometries, l = e.isWGS84 || e.isWebMercator ? i.executeMany(a, [this.distance], { union: this.unionResults, unit: this.unit }) : r.executeMany(a, [this.distance], { unit: this.unit, union: this.unionResults });
        this.bufferComplete.emit(l);
      }
    }, 400));
  }
  _getTextBoxDisplay() {
    return s`<div class="c-container"><calcite-input class="padding-end-1 w-50" .max=${this.max && this.max > 0 ? this.max : void 0} .min=${this.min} number-button-type=vertical @calciteInputInput=${this._setDistance} placeholder=0 type=number .value=${this.distance ? this.distance.toString() : void 0}></calcite-input><calcite-select class="flex-1 w-50" .label=${this._translations.unit} @calciteSelectChange=${() => {
      this._unitElement?.value && this._setUnit(this._unitElement?.value.value);
    }} ${d(this._unitElement)}>${this._getUnits()}</calcite-select></div>`;
  }
  _getSliderDisplay() {
    return s`<div><calcite-slider label-handles .max=${this.max && this.max > 0 ? this.max : void 0} .min=${this.min} .ticks=${this.sliderTicks}></calcite-slider></div>`;
  }
  _testAccess(t, e) {
    switch (t) {
      case "_setUnit":
        return this._setUnit(e);
      case "_setDistance":
        return this._setDistance(e);
    }
    return null;
  }
}
h("buffer-tools", p);
export {
  p as BufferTools
};
