import { c as S } from "../../chunks/runtime.js";
import { html as n } from "lit-html";
import { ref as f } from "lit-html/directives/ref.js";
import { g as _ } from "../../chunks/globalCss.js";
import C from "@geoscene/core/views/Theme.js";
import { u as I } from "../../chunks/useT9n.js";
import { generateGuid as M, classes as h } from "@arcgis/components-utils";
import { usePropertyChange as T } from "@arcgis/lumina/controllers";
import { m as E } from "../../chunks/useViewModel.js";
import g from "@geoscene/core/Color.js";
import { LitElement as x, createEvent as P, noShadowRoot as k, safeClassMap as d, nothing as u } from "@arcgis/lumina";
import { css as R } from "@lit/reactive-element/css-tag.js";
import { substitute as O } from "@geoscene/core/intl.js";
import { watch as D } from "@geoscene/core/core/reactiveUtils.js";
import L from "@geoscene/core/widgets/support/GridControls/GridControlsViewModel.js";
/*! All material copyright GeoScene, All Rights Reserved, unless otherwise specified.
See https://js.geoscene.cn/4.33/geoscene/copyright.txt for details.
v4.33.18 */
function B(s) {
  if (s == null || Number.isNaN(s))
    return 0;
  const e = s + 180;
  return Math.trunc(y(e, 360));
}
function N(s) {
  let e = s;
  return e == null || (typeof e == "string" && (e = Number.parseInt(e, 10)), Number.isNaN(e)) ? 0 : y(Math.trunc(e), 360) - 180;
}
function y(s, e) {
  return (s % e + e) % e;
}
const $ = {
  fromAttribute: (s) => g.fromHex(s),
  toAttribute: (s) => typeof s == "string" ? s : s?.toHex() ?? ""
}, W = R`@layer{calcite-block>.geoscene-grid-controls{background:none;padding:0}.geoscene-ui-corner .geoscene-expand .geoscene-widget--panel .geoscene-widget--panel.geoscene-grid-controls{overflow-y:visible}.geoscene-grid-controls{user-select:none;display:flex;flex-flow:column wrap;padding:var(--calcite-spacing-sm);.container{display:flex;flex:1 1 auto;flex-direction:column;gap:var(--calcite-spacing-sm)}.numeric-inputs-container{display:grid;grid-auto-columns:1fr;grid-auto-flow:column;gap:var(--calcite-spacing-sm)}geoscene-labeled-switch{margin-block-end:var(--calcite-spacing-sm)}.switch--bordered{border-width:0 0 var(--calcite-spacing-px) 0;border-style:solid;border-color:var(--calcite-color-border-1);padding-block-end:var(--calcite-spacing-sm)}.tile-group{display:flex;column-gap:var(--calcite-spacing-md);align-items:start;margin-bottom:calc(0px - var(--calcite-spacing-sm))}.tile{position:relative;.content{display:flex;flex-direction:column;row-gap:var(--calcite-spacing-md);align-items:center;padding:var(--calcite-spacing-md);color:var(--calcite-color-text-2)}&.disabled{opacity:var(--calcite-opacity-disabled)}&:focus-within{outline:var(--calcite-spacing-base) solid var(--calcite-color-brand);outline-offset:var(--calcite-spacing-base)}input[type=radio]{appearance:none;position:absolute;inset:0;opacity:.001;border:var(--calcite-spacing-px) solid transparent;cursor:pointer;&:checked+.content{border:var(--calcite-spacing-px) solid var(--calcite-color-brand)}+.content{border:var(--calcite-spacing-px) solid transparent;calcite-label{margin-bottom:calc(0px - var(--calcite-label-margin-bottom, .75rem));font-weight:var(--calcite-font-weight-medium)}}}}.inline-icon{margin-left:var(--calcite-spacing-xs);vertical-align:top;color:var(--calcite-color-status-warning)}}}`, G = "geoscene-grid-controls", r = {
  base: G,
  borderedSwitch: "switch--bordered",
  container: "container",
  numericInputsContainer: "numeric-inputs-container",
  tileGroup: "tile-group",
  tile: "tile",
  tileDisabled: "disabled",
  tileContent: "content",
  inlineIcon: "inline-icon"
}, m = {
  componentIcon: "grid-unit",
  gridTheme: { light: "circle", dark: "circle-area", custom: "palette" },
  placementState: { interactive: "maximum-territory-distance", place: "move", rotate: "rotate" },
  warning: "exclamation-mark-triangle"
}, b = g.fromArray([115, 115, 115]), v = g.fromArray([200, 200, 200]), A = ["interactive", "place", "rotate"], j = E(L);
class V extends x {
  constructor() {
    super(...arguments), this._labelUid = M(), this._messages = I({}), this._viewModel = j(this), this._placementModeButtonRefs = /* @__PURE__ */ new Map(), this.numericSpacingInputShouldBeVisible = this._viewModel.numericSpacingInputShouldBeVisible, this.position = "bottom-right", this.icon = m.componentIcon, this.snappingOptions = this._viewModel.snappingOptions, this.hideColorSelection = !1, this.hideDynamicScaleToggle = !1, this.hideGridEnabledToggle = !1, this.hideNumericInputs = !1, this.hideGridSnapEnabledToggle = !1, this.hideLineIntervalInput = !1, this.hideOutOfScaleWarning = !1, this.hidePlacementButtons = !1, this.hideRotateWithMapToggle = !1, this.rotation = this._viewModel.rotation, this.rotateWithMap = this._viewModel.rotateWithMap, this.spacing = this._viewModel.spacing, this.majorLineInterval = this._viewModel.majorLineInterval, this.gridColor = this._viewModel.gridColor, this.displayEnabled = this._viewModel.displayEnabled, this.gridOutOfScale = this._viewModel.gridOutOfScale, this.unit = this._viewModel.unit, this.dynamicScaling = this._viewModel.dynamicScaling, this.gridControlsEnabled = this._viewModel.gridControlsEnabled, this.interactivePlacementState = this._viewModel.interactivePlacementState, this.snappingEnabled = this._viewModel.snappingEnabled, this.effectiveSpacingAfterDynamicScaling = this._viewModel.effectiveSpacingAfterDynamicScaling, this.placementDisabled = this._viewModel.placementDisabled, this.autoDestroyDisabled = !1, this.geoscenePropertyChange = T()("customColor", "theme", "spacing", "rotation", "majorLineInterval", "rotateWithMap", "dynamicScaling", "snappingEnabled", "interactivePlacementState", "displayEnabled", "gridOutOfScale", "effectiveSpacingAfterDynamicScaling", "gridControlsEnabled", "gridColor"), this.geosceneReady = P();
  }
  static {
    this.properties = { _placementModeButtonRefs: 16, _warningIconTooltipRef: 16, _themePopoverRef: 16, numericSpacingInputShouldBeVisible: 16, referenceElement: 1, position: 3, icon: 1, customColor: [3, { converter: $ }], snappingOptions: 0, theme: 1, hideColorSelection: 5, hideDynamicScaleToggle: 5, hideGridEnabledToggle: 5, hideNumericInputs: 5, hideGridSnapEnabledToggle: 5, hideLineIntervalInput: 5, hideOutOfScaleWarning: 5, hidePlacementButtons: 5, hideRotateWithMapToggle: 5, rotation: 11, rotateWithMap: 5, spacing: 11, majorLineInterval: 11, gridColor: [3, { converter: $ }], displayEnabled: 4, gridOutOfScale: 7, unit: 1, dynamicScaling: 5, gridControlsEnabled: 4, interactivePlacementState: 3, snappingEnabled: 5, effectiveSpacingAfterDynamicScaling: 43, placementDisabled: 5, autoDestroyDisabled: 5, messageOverrides: 0, label: 1 };
  }
  static {
    this.styles = W;
  }
  static {
    this.shadowRootOptions = k;
  }
  get theme() {
    const e = this.gridColor;
    return !e || b.equals(e) ? "dark" : v.equals(e) ? "light" : "custom";
  }
  set theme(e) {
    const t = this._viewModel.view;
    switch (e) {
      case "dark":
        this.gridColor = b;
        break;
      case "custom":
        this.customColor == null && (this.customColor = t?.theme?.accentColor ?? new C().accentColor), this.gridColor = this.customColor;
        break;
      case "light":
      default:
        this.gridColor = v;
        break;
    }
  }
  async destroy() {
    await this.manager.destroy();
  }
  trySetDisplayEnabled(e) {
    this._viewModel.trySetDisplayEnabled(e);
  }
  loaded() {
    this.manager.onLifecycle(() => [
      // Move focus when movement angle changed by touch/mouse (not keyboard!)
      D(() => this._viewModel.gridColor, (e) => {
        const t = e instanceof g ? e : typeof e == "string" ? g.fromHex(e) : null;
        t && !b.equals(e) && !v.equals(t) ? this.customColor = t : b.equals(t) ? this.theme = "dark" : v.equals(t) && (this.theme = "light");
      })
    ]);
  }
  _togglePlacementState(e) {
    this.interactivePlacementState = this.interactivePlacementState === e ? null : e;
  }
  _setWarningIconRef(e) {
    this._warningIconTooltipRef = e;
  }
  _setPopoverRef(e) {
    this._themePopoverRef = e;
  }
  _emptyFunction(e) {
  }
  _setPlacementButtonRef(e) {
    if (e) {
      const t = e.dataset.placementAction;
      this._placementModeButtonRefs.set(t, e);
    } else
      this._placementModeButtonRefs.clear();
  }
  render() {
    return n`<div class=${d(h(r.base, _.widget, _.panel))}><div class=${d(r.container)}>${this._renderContent()}</div></div>`;
  }
  _renderContent() {
    const { hideGridEnabledToggle: e, hidePlacementButtons: t, hideNumericInputs: i, hideLineIntervalInput: a, hideGridSnapEnabledToggle: l, hideDynamicScaleToggle: o, hideRotateWithMapToggle: c, hideColorSelection: p } = this;
    return [
      e ? void 0 : this._renderEnabledToggle(),
      t ? void 0 : this._renderGridPlacementButtons(),
      i ? void 0 : this._renderNumericInputs(),
      a ? void 0 : this._renderIntervalInput(),
      !l && this.snappingOptions ? this._renderSnappingToggle() : void 0,
      o ? void 0 : this._renderDynamicScaleToggle(),
      c ? void 0 : this._renderRotateToggle(),
      p ? void 0 : this._renderColorSelection()
    ];
  }
  _renderGridPlacementButtons() {
    const { interactivePlacementState: e, placementDisabled: t, gridControlsEnabled: i } = this, a = t || !i;
    return n`<calcite-label scale=s>${this._messages.placementOptions}<calcite-action-bar expand-disabled layout=horizontal>${A.map((l) => this._renderPlacementModeButton(l, a, e))}</calcite-action-bar></calcite-label>`;
  }
  _renderPlacementModeButton(e, t, i) {
    const a = this._messages?.placementState?.[e], l = this._placementModeButtonRefs.get(e);
    return n`<div><calcite-action .active=${i === e} alignment=center .disabled=${t} .icon=${m.placementState[e]} data-placement-action=${e ?? u} @click=${() => this._togglePlacementState(e)} .text=${a ?? ""} ${f(this._setPlacementButtonRef)}></calcite-action>${l ? n`<calcite-tooltip overlay-positioning=fixed placement=top .referenceElement=${l}>${a}</calcite-tooltip>` : void 0}</div>`;
  }
  _renderNumericInputs() {
    const { rotation: e, spacing: t, effectiveSpacingAfterDynamicScaling: i, numericSpacingInputShouldBeVisible: a, gridControlsEnabled: l } = this, o = i != null && i !== t;
    return n`<div class=${d(h(r.numericInputsContainer))}>${[
      a ? n`<calcite-label scale=s><span>${this._messages?.inputLabel?.spacing}${o ? n`<calcite-icon class=${d(h(r.inlineIcon))} .icon=${m.warning} scale=s tabindex=0 ${f(this._setWarningIconRef)}></calcite-icon>` : void 0}</span><calcite-input-number .disabled=${!l} number-button-type=none .value=${t?.toFixed(2)} @calciteInputNumberChange=${({ currentTarget: c }) => {
        const p = Number.parseFloat(c.value);
        this.spacing = p, p !== this.spacing && (c.value = `${this.spacing}`);
      }}></calcite-input-number>${o && this._warningIconTooltipRef ? n`<calcite-tooltip overlay-positioning=fixed placement=leading .referenceElement=${this._warningIconTooltipRef}>${O(this._messages?.warnings?.dynamicSpacing ?? "", { actualSpacingIncludingUnit: i }, {
        format: {
          actualSpacingIncludingUnit: {
            type: "number",
            intlOptions: {
              minimumFractionDigits: 2,
              maximumFractionDigits: 2,
              unit: this.unit ?? "meters",
              style: "decimal"
            }
          }
        }
      })}</calcite-tooltip>` : void 0}</calcite-label>` : void 0,
      n`<calcite-label scale=s>${this._messages?.inputLabel?.angle}<calcite-input-number .disabled=${!l} integer number-button-type=none suffix-text=\u00B0 .value=${`${B(e)}`} @calciteInputNumberChange=${({ currentTarget: c }) => this.rotation = N(c.value)}></calcite-input-number></calcite-label>`
    ]}</div>`;
  }
  _renderIntervalInput() {
    const { majorLineInterval: e, gridControlsEnabled: t } = this;
    return n`<div class=${d(h(r.numericInputsContainer))}><calcite-label scale=s>${this._messages?.inputLabel?.interval}<calcite-input-number .disabled=${!t} integer max=15 min=1 number-button-type=vertical .value=${`${e}`} @calciteInputNumberChange=${({ currentTarget: i }) => {
      const a = Number.parseInt(i.value, 10);
      this.majorLineInterval = a, a !== this.majorLineInterval && (i.value = `${this.majorLineInterval}`);
    }}></calcite-input-number></calcite-label></div>`;
  }
  _renderSnappingToggle() {
    const { snappingOptions: e, gridControlsEnabled: t } = this, i = e?.gridEnabled ?? !1;
    return n`<geoscene-labeled-switch .checked=${!!i} .disabled=${!(t && !!e)} .label=${this._messages?.switchLabel?.snap} @geosceneCheckedChanged=${(l) => {
      l.stopPropagation(), this.snappingOptions?.set("gridEnabled", l.detail);
    }}></geoscene-labeled-switch>`;
  }
  _renderDynamicScaleToggle() {
    const { _messages: e, gridOutOfScale: t, dynamicScaling: i, gridControlsEnabled: a } = this, l = !this.hideOutOfScaleWarning && t;
    return n`<geoscene-labeled-switch .checked=${i} .disabled=${!a} .hint=${l ? e?.warnings?.outOfScale : void 0} .hintIcon=${m.warning} hint-kind=warning .label=${e?.switchLabel?.scaling} @geosceneCheckedChanged=${(o) => {
      o.stopPropagation(), this.dynamicScaling = o.detail;
    }}></geoscene-labeled-switch>`;
  }
  _renderRotateToggle() {
    const { gridControlsEnabled: e, rotateWithMap: t } = this;
    return n`<geoscene-labeled-switch .checked=${t} .disabled=${!e} .label=${this._messages?.switchLabel?.rotateWithMap ?? ""} @geosceneCheckedChanged=${(i) => {
      i.stopPropagation(), this.rotateWithMap = i.detail;
    }}></geoscene-labeled-switch>`;
  }
  _renderEnabledToggle() {
    return n`<div class=${d(r.borderedSwitch)}><geoscene-labeled-switch .checked=${this.displayEnabled} .disabled=${!!this.interactivePlacementState} .label=${this._messages?.switchLabel?.display} @geosceneCheckedChanged=${(e) => {
      e.stopPropagation(), this.trySetDisplayEnabled(e.detail);
    }}></geoscene-labeled-switch></div>`;
  }
  _renderColorSelection() {
    const { customColor: e } = this;
    return n`<calcite-label scale=s>${this._messages?.inputLabel?.theme}<div class=${d(r.tileGroup)}>${[
      this._renderThemeTile({ value: "dark" }),
      this._renderThemeTile({ value: "light" }),
      this._renderThemeTile({ value: "custom" }),
      this._themePopoverRef ? n`<calcite-popover auto-close .label=${this._messages?.gridLineColorPopoverLabel ?? ""} overlay-positioning=fixed .referenceElement=${this._themePopoverRef}><calcite-color-picker alpha-channel saved-disabled .value=${e?.toHex({ digits: 8 }) ?? "#000000ff"} @calciteColorPickerChange=${(t) => {
        const i = g.fromHex(t.currentTarget.value);
        i && (this.customColor = i, this.theme = "custom");
      }}></calcite-color-picker></calcite-popover>` : void 0
    ]}</div></calcite-label>`;
  }
  _renderThemeTile(e) {
    const { theme: t, gridControlsEnabled: i } = this, { value: a } = e, l = this._messages?.gridTheme?.[a], o = `${this._labelUid}--theme_tile_${a}`, c = `${o}__label`, p = m.gridTheme[a];
    return n`<div class=${d(h({ [r.tile]: !0, [r.tileDisabled]: !i }))} role=button ${f(a === "custom" ? this._setPopoverRef : this._emptyFunction)}><input aria-labelledby=${c ?? u} .checked=${t === a} .disabled=${!i} id=${o ?? u} .name=${`${this._labelUid}--theme`} @change=${(w) => {
      this.theme = a, w.currentTarget.focus();
    }} tabindex=0 type=radio><div class=${d(r.tileContent)}><calcite-icon .icon=${p} scale=l></calcite-icon><calcite-label .for=${o} id=${c ?? u}>${l}</calcite-label></div></div>`;
  }
}
S("geoscene-grid-controls", V);
export {
  V as GeosceneGridControls
};