/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { c as x } from "../../chunks/runtime.js";
import { css as _, html as d } from "lit";
import { classes as g } from "@arcgis/toolkit/dom";
import C from "@arcgis/core/widgets/Swipe/SwipeViewModel.js";
import { LitElement as L, createEvent as v, safeClassMap as u, safeStyleMap as w, nothing as D } from "@arcgis/lumina";
import { usePropertyChange as M } from "@arcgis/lumina/controllers";
import { createRef as P, ref as $ } from "lit/directives/ref.js";
import { u as K } from "../../chunks/useT9n.js";
import { m as A } from "../../chunks/useViewModel.js";
import { g as m } from "../../chunks/globalCss.js";
import { l as b } from "../../chunks/legacyIcon.js";
const S = _`:host{position:absolute;margin:0;background:transparent!important;padding:0;width:100%;height:100%;overflow:hidden;user-select:none;--arcgis-internal-swipe-handle-color-background: var(--calcite-color-background);--arcgis-internal-swipe-divider-color-background: var(--calcite-color-background)}.root{width:100%;height:100%;overflow:hidden;user-select:none}.root,.arcgis-ui .root{pointer-events:none!important}.container{position:absolute;z-index:1;margin:0;outline:0;border:0;padding:0;overflow:hidden;pointer-events:auto;touch-action:none;outline-color:transparent}.horizontal .container{margin-left:calc(var(--calcite-spacing-xxxl) / 2 * -1);cursor:col-resize;height:100%}.vertical .container{margin-top:calc(var(--calcite-spacing-xxxl) / 2 * -1);cursor:row-resize;width:100%}.disabled .container{cursor:default;pointer-events:none}.divider{position:absolute;background-color:var(--arcgis-internal-swipe-divider-color-background)}.horizontal .divider{top:0;left:calc(var(--calcite-spacing-xxxl) / 2);margin-left:calc(var(--calcite-border-width-lg) * .5 * -1);border-right:var(--calcite-border-width-sm) solid var(--calcite-color-border-1);border-left:var(--calcite-border-width-sm) solid var(--calcite-color-border-1);width:var(--calcite-border-width-lg);height:100%}.vertical .divider{top:calc(var(--calcite-spacing-xxxl) / 2);left:0;margin-top:calc(var(--calcite-border-width-lg) * .5 * -1);border-top:var(--calcite-border-width-sm) solid var(--calcite-color-border-1);border-bottom:var(--calcite-border-width-sm) solid var(--calcite-color-border-1);width:100%;height:var(--calcite-border-width-lg)}.handle{display:flex;position:relative;align-items:center;justify-content:center;border:var(--calcite-border-width-sm) solid var(--calcite-color-border-1);border-radius:var(--calcite-corner-radius-round);background-color:var(--arcgis-internal-swipe-handle-color-background);width:var(--calcite-spacing-xxxl);height:var(--calcite-spacing-xxxl);color:var(--calcite-color-text-3)}.handle-hidden{visibility:hidden}.vertical .handle{rotate:90deg}.handle-icon{position:relative;z-index:2}.handle-icon:before{content:"|||";color:inherit}.horizontal .handle{top:calc(50% - calc(var(--calcite-spacing-xxxl) / 2))}.vertical .handle{left:calc(50% - calc(var(--calcite-spacing-xxxl) / 2))}.container:focus .handle,.container:focus .divider{outline:inset 2px var(--calcite-color-focus, var(--calcite-color-focus, var(--calcite-color-brand)))}.container:focus .handle{outline-offset:calc(-2px*(1 - (2*clamp(0,var(--calcite-offset-invert-focus),1))))}`, k = A(C);
class z extends L {
  constructor() {
    super(...arguments), this._container = null, this._containerRef = P(), this._swiping = !1, this._positionChanged = !1, this.messages = K({}), this._pointerOffset = 0, this.viewModel = k(this), this._onContainerPointerDown = (e) => {
      e.preventDefault();
      const { _container: i } = this;
      i && document.activeElement !== this.el && i.focus(), this._swiping = !0, this._calculatePointerOffset(e);
    }, this.view = this.viewModel.view, this.autoDestroyDisabled = !1, this.direction = this.viewModel.direction, this.disabled = !1, this.hideDivider = !1, this.hideHandle = !1, this.icon = "compare", this.label = this.messages.componentLabel, this.position = this.viewModel.position, this.state = this.viewModel.state, this.arcgisPropertyChange = M()("state"), this.arcgisReady = v(), this.arcgisSwipeChange = v(), this.arcgisSwipeInput = v();
  }
  static {
    this.properties = { view: 0, autoDestroyDisabled: 5, direction: 3, disabled: 7, endLayers: 0, hideDivider: 7, hideHandle: 7, icon: 1, label: 1, leadingLayers: 0, messageOverrides: 0, position: 9, startLayers: 0, swipePosition: 9, referenceElement: 1, state: 32, trailingLayers: 0 };
  }
  static {
    this.styles = S;
  }
  get endLayers() {
    return this.viewModel.trailingLayers;
  }
  set endLayers(e) {
    this.viewModel.trailingLayers = e;
  }
  get leadingLayers() {
    return this.startLayers;
  }
  set leadingLayers(e) {
    this.startLayers = e;
  }
  get startLayers() {
    return this.viewModel.leadingLayers;
  }
  set startLayers(e) {
    this.viewModel.leadingLayers = e;
  }
  get swipePosition() {
    return this.position;
  }
  set swipePosition(e) {
    this.position = e;
  }
  get trailingLayers() {
    return this.endLayers;
  }
  set trailingLayers(e) {
    this.endLayers = e;
  }
  async destroy() {
    await this.manager.destroy();
  }
  loaded() {
    this.listenOn(document, "pointermove", this._onContainerPointerMove), this.listenOn(document, "pointerup", this._onContainerPointerUp);
  }
  _calculatePointerOffset(e) {
    const { direction: i } = this, t = this._containerRef.value, r = (i === "vertical" ? t.clientHeight : t.clientWidth) / 2, o = t.getBoundingClientRect(), n = e.clientX - o.left, c = e.clientY - o.top;
    this._pointerOffset = i === "vertical" ? c - r : n - r;
  }
  _onContainerPointerUp(e) {
    this._swiping && (e.preventDefault(), this._positionChanged && (this.arcgisSwipeChange.emit(), this._positionChanged = !1), this._swiping = !1);
  }
  _onContainerPointerMove(e) {
    if (!this._swiping)
      return;
    e.preventDefault();
    const { _pointerOffset: i, el: t, direction: r } = this, { clientX: o, clientY: n } = e, { top: c, left: s, width: a, height: l } = t.getBoundingClientRect(), f = r === "vertical" ? l : a, h = (r === "vertical" ? n - c - i : o - s - i) / f * 100;
    this.position !== h && (this.position = h, this._positionChanged = !0, this.arcgisSwipeInput.emit());
  }
  _getKeyPosition(e) {
    const { key: i } = e, { position: t } = this, { max: r, min: o, step: n, stepMultiplier: c, direction: s } = this.viewModel, a = n * c;
    if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Home", "End", "PageUp", "PageDown"].includes(i) && (e.preventDefault(), e.stopPropagation()), s === "vertical" ? i === "ArrowDown" || i === "ArrowRight" : i === "ArrowUp" || i === "ArrowRight") {
      const p = e.shiftKey ? a : n;
      return t + p;
    }
    if (s === "vertical" ? i === "ArrowUp" || i === "ArrowLeft" : i === "ArrowDown" || i === "ArrowLeft") {
      const p = e.shiftKey ? a : n;
      return t - p;
    }
    return i === "Home" ? o : i === "End" ? r : (s === "vertical" ? i === "PageDown" : i === "PageUp") ? t + a : (s === "vertical" ? i === "PageUp" : i === "PageDown") ? t - a : null;
  }
  _onContainerKeyDown(e) {
    const i = this._getKeyPosition(e);
    typeof i == "number" && this.position !== i && (this.position = i, this.arcgisSwipeInput.emit(), this.arcgisSwipeChange.emit());
  }
  render() {
    const { state: e, direction: i } = this.viewModel, t = e === "disabled" || this.disabled, r = {
      [m.disabled]: t,
      disabled: t,
      vertical: i === "vertical",
      horizontal: i === "horizontal"
    };
    return d`<div class=${u(g("root", m.widget, r))}>${e === "disabled" ? null : this._renderContainer()}</div>`;
  }
  _renderHandle() {
    const { direction: e } = this.viewModel, { hideHandle: i } = this, t = {
      [b.dragHorizontal]: e === "vertical",
      [b.dragVertical]: e === "horizontal"
    }, r = g("handle", !!i && "handle-hidden");
    return d`<div class=${u(r)} role=presentation><span aria-hidden=true class=${u(g("handle-icon", t))}></span></div>`;
  }
  _renderDivider() {
    return this.hideDivider ? null : d`<div class="divider" role=presentation></div>`;
  }
  _renderContent() {
    return [this._renderDivider(), this._renderHandle()];
  }
  _renderContainer() {
    const { disabled: e, viewModel: i } = this, { max: t, min: r, direction: o, position: n } = i, c = this.label ?? this.messages.dragLabel ?? "", s = `${n}%`, a = {
      top: o === "vertical" ? s : void 0,
      left: o === "vertical" ? void 0 : s
    }, l = this._renderContent();
    return e ? d`<div class="container" role=presentation style=${w(a)}>${l}</div>` : d`<div @pointerdown=${this._onContainerPointerDown} .ariaLabel=${c} .ariaOrientation=${o} .ariaValueMax=${`${t}`} .ariaValueMin=${`${r}`} .ariaValueNow=${`${n}`} .ariaValueText=${s} class="container" @keydown=${this._onContainerKeyDown} role=slider style=${w(a)} tabindex=0 title=${c ?? D} touch-action=none ${$(this._containerRef)}>${l}</div>`;
  }
}
x("arcgis-swipe", z);
export {
  z as ArcgisSwipe
};
