/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as customElement } from "../../chunks/runtime.js";
import { u as useT9n } from "../../chunks/useT9n.js";
import { css, html } from "lit";
import { createRef, ref } from "lit-html/directives/ref.js";
import { LitElement, createEvent } from "@arcgis/lumina";
import * as geodesicBufferOperator from "@arcgis/core/geometry/operators/geodesicBufferOperator.js";
import * as bufferOperator from "@arcgis/core/geometry/operators/bufferOperator.js";
const styles = css`/** @license
 * Copyright 2025 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

:host {
  display: block;
}

.c-container {
  display: inline-flex;
}

.flex-1 {
  flex: '1';
}

.padding-end-1 {
  padding-inline-end: 1rem;
}

.w-50 {
  width: 50%;
}`;
class BufferTools extends LitElement {
  constructor() {
    super(...arguments);
    this.appearance = "text";
    this.distance = 0;
    this.geometries = [];
    this.max = 0;
    this.min = 0;
    this.sliderTicks = 10;
    this.unionResults = true;
    this.unit = "meters";
    this.disabled = false;
    this._translations = useT9n({ name: "buffer-tools", blocking: true });
    this._unitElement = createRef();
    this.bufferComplete = createEvent();
    this.distanceChanged = createEvent();
    this.unitChanged = createEvent();
  }
  static {
    this.properties = { appearance: 1, distance: [9, {}, { type: Number }], geometries: [0, {}, { attribute: false }], max: [9, {}, { type: Number }], min: [9, {}, { type: Number }], sliderTicks: [9, {}, { type: Number }], unionResults: [5, {}, { type: Boolean }], unit: 1, disabled: [5, {}, { type: Boolean }], _translations: [16, {}, { state: true }], _units: [16, {}, { state: true }] };
  }
  static {
    this.styles = styles;
  }
  willUpdate(changes) {
    if (changes.has("geometries") && (this.hasUpdated || this.geometries?.length > 0)) {
      this.geometriesWatchHandler();
    }
    if (changes.has("disabled") && (this.hasUpdated || this.disabled !== false)) {
      this.disabledWatchHandler();
    }
    if (changes.has("_translations")) {
      void this._defineTranslations();
    }
    if (changes.has("unit") && this.hasUpdated) {
      this._setUnit(this.unit);
    }
    if (changes.has("distance") && this.hasUpdated) {
      this._setDistance({ target: { value: this.distance } });
    }
  }
  geometriesWatchHandler() {
    this._buffer();
  }
  disabledWatchHandler() {
    this._buffer();
  }
  async getTranslatedUnit(unit) {
    if (!this._units) {
      await this._defineTranslations();
    }
    return this._units ? this._units[unit] : "";
  }
  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((u) => {
      return this._units ? html`<calcite-option .label=${this._units[u]} .selected=${this.unit === u} .value=${u}></calcite-option>` : void 0;
    }) : void 0;
  }
  _setDistance(event) {
    const v = Number.parseInt(event.target.value, 10);
    if (this.distance !== v && v >= this.min) {
      this.distanceChanged.emit({
        oldValue: this.distance,
        newValue: v
      });
      this.distance = v;
      if (this.distance > 0) {
        this._buffer();
      } else {
        this.bufferComplete.emit(void 0);
      }
    }
  }
  _setUnit(unit) {
    this.unitChanged.emit({
      oldValue: this.unit,
      newValue: unit
    });
    this.unit = unit;
    this._buffer();
  }
  _buffer() {
    if (!this.disabled) {
      if (this._bufferTimeout) {
        clearTimeout(this._bufferTimeout);
      }
      this._bufferTimeout = setTimeout(async () => {
        if (this.geometries?.length > 0 && this.unit && this.distance > 0 && geodesicBufferOperator && bufferOperator) {
          if (!geodesicBufferOperator.isLoaded()) {
            await geodesicBufferOperator.load();
          }
          const geom = this.geometries[0];
          const sr = geom.spatialReference;
          const geoms = this.geometries;
          const buffer = sr.isWGS84 || sr.isWebMercator ? geodesicBufferOperator.executeMany(geoms, [this.distance], { union: this.unionResults, unit: this.unit }) : bufferOperator.executeMany(geoms, [this.distance], { unit: this.unit, union: this.unionResults });
          this.bufferComplete.emit(buffer);
        }
      }, 400);
    } else {
      this.bufferComplete.emit(void 0);
    }
  }
  _getTextBoxDisplay() {
    return html`<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=label @calciteSelectChange=${() => {
      if (this._unitElement?.value) {
        this._setUnit(this._unitElement?.value.value);
      }
    }} ${ref(this._unitElement)}>${this._getUnits()}</calcite-select></div>`;
  }
  _getSliderDisplay() {
    return html`<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(methodName, arg1) {
    switch (methodName) {
      case "_setUnit":
        return this._setUnit(arg1);
      case "_setDistance":
        return this._setDistance(arg1);
    }
    return null;
  }
}
customElement("buffer-tools", BufferTools);
export {
  BufferTools
};
