/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { c as O } from "../../chunks/runtime.js";
import { css as W, html as d } from "lit";
import { ref as F } from "lit/directives/ref.js";
import { useDirection as B, usePropertyChange as R } from "@arcgis/lumina/controllers";
import { classes as L } from "@arcgis/toolkit/dom";
import { LitElement as E, createEvent as H, nothing as _, safeClassMap as f, safeStyleMap as P } from "@arcgis/lumina";
import { u as V } from "../../chunks/useT9n.js";
import { m as D } from "../../chunks/useViewModel.js";
import { H as j } from "../../chunks/heading.js";
import { g as z } from "../../chunks/globalCss.js";
import I from "@arcgis/core/core/Accessor.js";
import { property as u, subclass as K } from "@arcgis/core/core/accessorSupport/decorators.js";
import { watch as q } from "@arcgis/core/core/reactiveUtils.js";
import S from "@arcgis/core/views/3d/environment/CloudyWeather.js";
import $ from "@arcgis/core/views/3d/environment/FoggyWeather.js";
import k from "@arcgis/core/views/3d/environment/RainyWeather.js";
import A from "@arcgis/core/views/3d/environment/SnowyWeather.js";
import T from "@arcgis/core/views/3d/environment/SunnyWeather.js";
const N = W`.esri-widget__heading{color:var(--calcite-color-text-2);margin:0 0 .5rem;font-weight:var(--calcite-font-weight-semibold)}h1.esri-widget__heading{font-size:var(--calcite-font-size-xl)}h2.esri-widget__heading{font-size:var(--calcite-font-size-md)}h3.esri-widget__heading,h4.esri-widget__heading,h5.esri-widget__heading,h6.esri-widget__heading{font-size:var(--calcite-font-size)}.esri-widget{box-sizing:border-box;color:var(--calcite-color-text-2);background-color:var(--calcite-color-foreground-1);font-size:var(--calcite-font-size);line-height:var(--calcite-font-line-height-relative-tight)}.esri-widget *,.esri-widget :before,.esri-widget :after{box-sizing:inherit}.esri-widget.esri-widget--disabled>*{opacity:.4;pointer-events:none}.esri-widget.esri-widget--compact{--esri-widget-padding-y: 0;--esri-widget-padding-x: 0}.esri-widget__anchor{color:var(--calcite-color-text-2)}.esri-widget__anchor:hover{color:var(--calcite-color-text-3)}.esri-widget__anchor:hover,.esri-widget__anchor:focus{text-decoration:underline}.esri-widget__anchor--disabled{opacity:.4;pointer-events:none}.esri-widget__header{background-color:var(--calcite-color-foreground-1);flex:1 0 100%;justify-content:space-between;align-items:center;width:100%;min-height:48px;padding:12px 0 12px 15px;display:flex}.esri-widget__heading{word-break:break-word}.esri-widget__header .esri-widget__heading{width:calc(100% - 48px);text-align:initial;color:var(--calcite-color-text-1);align-items:center;margin:0;padding:0;display:flex}.esri-widget__header-button{appearance:none;cursor:pointer;color:var(--calcite-color-text-2);background-color:var(--calcite-color-foreground-1);border:none;flex:0 0 48px;justify-content:center;align-self:stretch;align-items:center;width:48px;padding:0;display:flex}.esri-widget__footer{background-color:var(--calcite-color-foreground-1);justify-content:center;align-items:center;width:100%;min-height:48px;display:flex}.esri-widget__footer-pagination{color:var(--calcite-color-text-2);justify-content:space-between;align-items:center;display:flex}.esri-widget__footer-pagination-previous-button,.esri-widget__footer-pagination-next-button{cursor:pointer;background-color:var(--calcite-color-foreground-1);border:0;margin:0 7px;padding:12px 15px;transition:background-color .125s ease-in-out}.esri-widget__footer-pagination-previous-button:hover,.esri-widget__footer-pagination-previous-button:focus,.esri-widget__footer-pagination-next-button:hover,.esri-widget__footer-pagination-next-button:focus{color:var(--calcite-color-text-1);background-color:var(--calcite-color-foreground-1)}.esri-menu{visibility:hidden;z-index:1;background-clip:padding;background-color:var(--calcite-color-foreground-1);background-clip:padding-box;width:100%;max-height:0;margin:2px 0 0;padding:0;font-size:var(--calcite-font-size);line-height:var(--calcite-spacing-xl);position:absolute;top:100%;left:0;overflow:hidden;box-shadow:0 1px 2px #0000004d}.esri-menu .esri-menu__header{color:var(--calcite-color-text-inverse);background-color:var(--calcite-color-inverse);padding:6px 12px}.esri-menu__list{margin:0;padding:0;list-style:none}.esri-menu__list-item{cursor:pointer;border-top:1px solid #adadad4d;border-left:3px solid #0000;padding:.8em 1em}.esri-menu__list-item [class^=esri-icon-],.esri-menu__list-item [class*=" esri-icon-"]{padding-right:2.8px}.esri-menu__list-item:first-child{border-top:none}.esri-menu__list-item--focus,.esri-menu__list-item:hover,.esri-menu__list-item:focus{background-color:var(--calcite-color-foreground-1)}.esri-menu__list-item:active{background-color:var(--calcite-color-foreground-current)}.esri-menu__list-item--active,.esri-menu__list-item--active:hover,.esri-menu__list-item--active:focus{background-color:var(--calcite-color-foreground-1);border-left-color:var(--calcite-color-brand)}.esri-widget__table{border-collapse:collapse;color:var(--calcite-color-text-1);border:none;width:100%;line-height:var(--calcite-font-line-height-relative-tight)}.esri-widget__table tr:nth-child(odd){background-color:var(--calcite-color-transparent)}.esri-widget__table tr:nth-child(2n){background-color:var(--calcite-color-transparent-press)}.esri-widget__table tr a{color:var(--calcite-color-text-2)}.esri-widget__table tr a:hover,.esri-widget__table tr a:focus{color:var(--calcite-color-text-1)}.esri-widget__table tr td,.esri-widget__table tr th{vertical-align:top;word-break:break-word;padding:.5em .7em;font-size:var(--calcite-font-size-sm);font-weight:var(--calcite-font-weight-regular)}.esri-widget__table tr th{text-align:left;border-right:3px solid #0000000d;width:50%}.esri-widget__table tr td{width:50%}.esri-input{color:var(--calcite-color-text-1);background-color:var(--calcite-color-foreground-1);border:1px solid #959595;font-size:var(--calcite-font-size)}.esri-input::-ms-clear{display:none}.esri-input::placeholder{opacity:1;color:var(--calcite-color-text-2)}.esri-input:placeholder-shown{text-overflow:ellipsis}.esri-input[type=text],.esri-input[type=password],.esri-input[type=number]{height:32px;padding:0 .5em}calcite-combobox.esri-input{border:none}.esri-widget__content--empty{text-align:center;color:var(--calcite-color-border-input);flex-flow:column wrap;align-items:center;padding:18px 22px;display:flex}.esri-widget__content--empty h1.esri-widget__heading,.esri-widget__content--empty h2.esri-widget__heading,.esri-widget__content--empty h3.esri-widget__heading,.esri-widget__content--empty h4.esri-widget__heading,.esri-widget__content--empty h5.esri-widget__heading{font-weight:var(--calcite-font-weight-regular)}.esri-widget__content-illustration--empty{width:128px;padding:1rem 0}.esri-widget__content-icon--empty{width:32px;padding:.5rem 0}.esri-select{appearance:none;cursor:pointer;color:var(--calcite-color-text-2);background:var(--calcite-color-foreground-1) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='-8 -8 32 32' width='32' height='32'%3E%3Cpath d='M8 11.207l-4-4V5.793l4 4 4-4v1.414z' fill='%23adadad' fillrule='nonzero'/%3E%3C/svg%3E") 100% no-repeat;border:1px solid #959595;border-radius:0;width:100%;height:32px;margin:0;padding:0 .5em;font-family:inherit;font-size:.85em;display:block}.esri-select[disabled]{cursor:auto}.esri-select::-ms-expand{display:none}.esri-disabled a,.esri-disabled [role=menu],.esri-disabled [role=checkbox],.esri-disabled [class^=esri-icon-],.esri-disabled [class*=" esri-icon-"]{color:var(--calcite-color-text-3)}.keynav-mode .esri-widget:focus,.keynav-mode .esri-widget :focus{z-index:1;outline-offset:2px;outline:2px solid #adadad}.esri-widget__loader-animation{border:.15em solid #adadad4d;border-top-color:var(--calcite-color-border-input);border-radius:100%;width:1em;height:1em;font-size:var(--calcite-font-size-md);animation:1.25s linear infinite esri-rotate;display:block;transform:translateZ(0)}:dir(rtl) .esri-widget__table th{text-align:right;border-left:3px solid #0000000d;border-right:none}:dir(rtl) .esri-select{background-position:0}:dir(rtl) .esri-menu__list-item{border-left:none;border-right:3px solid #0000}:dir(rtl) .esri-menu__list-item [class^=esri-icon-],:dir(rtl) .esri-menu__list-item [class*=" esri-icon-"]{padding-left:2px;padding-right:0}:dir(rtl) .esri-menu__list-item--active{border-right-color:var(--calcite-color-brand)}.esri-icon-font-fallback-text{clip:rect(0 0 0 0);width:1px;height:1px;position:absolute;overflow:hidden}.esri-rotating{animation:1.25s linear infinite esri-rotate;transform:translateZ(0)}.esri-clearfix:before,.esri-clearfix:after{content:" ";display:table}.esri-clearfix:after{clear:both}.esri-interactive{cursor:pointer}.esri-hidden{display:none!important}.esri-invisible{visibility:hidden!important}.esri-offscreen{position:absolute;top:-999em;left:-999em}.esri-match-height{flex-direction:column;flex:auto;display:flex;overflow:hidden}:host{width:300px;min-width:180px}.root{position:relative;padding:var(--arcgis-internal-padding);container-type:inline-size;width:100%}.content{display:flex;flex-direction:column;gap:var(--calcite-spacing-lg)}.content.error{visibility:hidden;height:0;overflow:hidden;pointer-events:none}.weather-type-selector{--button-gap: var(--calcite-spacing-sm);--button-size-min: 24px;--button-size-s-max: 32px;--button-size-m-max: 64px;display:flex;flex-wrap:wrap;align-items:center;justify-content:center;width:100%;min-block-size:var(--button-row-min-height, 0);margin-inline:auto;gap:var(--button-gap);>calcite-button{display:block;position:relative;flex-shrink:0;flex-grow:0;aspect-ratio:1 / 1;height:fit-content;--total-gap: calc((var(--weather-type-count) - 1) * var(--button-gap));--basis: clamp( var(--button-size-min), calc((100% - var(--total-gap)) / var(--weather-type-count)), var(--button-size-m-max) );flex-basis:round(down,var(--basis),1px)}}@container (max-width: 280px){.weather-type-selector{--button-gap: var(--calcite-spacing-xs)}}@container (max-width: 220px){.weather-type-selector{--button-gap: var(--calcite-spacing-xxs)}}.options{display:flex;flex-direction:column;gap:var(--calcite-spacing-md);calcite-label{--calcite-label-margin-bottom: 0}&.hidden{display:none}}.warning-title{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0}`;
var G = Object.defineProperty, Z = Object.getOwnPropertyDescriptor, g = (o, e, t, i) => {
  for (var r = i > 1 ? void 0 : i ? Z(e, t) : e, n = o.length - 1, a; n >= 0; n--)
    (a = o[n]) && (r = (i ? a(e, t, r) : a(r)) || r);
  return i && r && G(e, t, r), r;
};
const U = {
  sunny: T,
  cloudy: S,
  rainy: k,
  snowy: A,
  foggy: $
}, p = Object.keys(U);
let l = class extends I {
  //#region Lifecycle
  constructor(o) {
    super(o), this._settingWeather = !1, this._weatherByType = {
      sunny: new T(),
      cloudy: new S(),
      rainy: new k(),
      snowy: new A(),
      foggy: new $()
    };
  }
  initialize() {
    this.addHandles([
      // When the weather is changed directly in the environment (not in the VM),
      // we need to update the corresponding weather instance in the view model.
      q(
        () => this.current,
        (o) => {
          this._settingWeather || o == null || (this._weatherByType = {
            ...this._weatherByType,
            [o.type]: o
          });
        },
        { sync: !0, initial: !0 }
      )
    ]);
  }
  get state() {
    return this.view?.ready ? this.error != null ? this.error === "not-visible" ? "ready" : "error" : "ready" : "disabled";
  }
  get disabled() {
    return this.state === "disabled";
  }
  get weatherByType() {
    return this._weatherByType;
  }
  get error() {
    const o = this.view;
    return o?.ready ? o.type !== "3d" ? "unsupported-view" : o.viewingMode === "local" ? "local-scene" : o.environment.atmosphereEnabled ? o.environment.weatherAvailable ? null : "not-visible" : "no-atmosphere" : null;
  }
  get current() {
    return this._environment?.weather ?? this._weatherByType.sunny;
  }
  set current(o) {
    const e = this._environment;
    e && (e.weather = o);
  }
  get _environment() {
    const o = this.view;
    return o?.type === "3d" ? o.environment : null;
  }
  //#endregion
  //#region Public Methods
  setWeatherByType(o) {
    const e = this._environment;
    e != null && (this._settingWeather = !0, e.weather = this._weatherByType[o], this._settingWeather = !1);
  }
  setSnowCoverEnabled(o) {
    const e = this.current;
    "snowCover" in e && (e.snowCover = o ? "enabled" : "disabled");
  }
  //#endregion
};
g([
  u()
], l.prototype, "view", 2);
g([
  u()
], l.prototype, "state", 1);
g([
  u()
], l.prototype, "disabled", 1);
g([
  u()
], l.prototype, "weatherByType", 1);
g([
  u()
], l.prototype, "error", 1);
g([
  u()
], l.prototype, "current", 1);
g([
  u()
], l.prototype, "_weatherByType", 2);
g([
  u()
], l.prototype, "_environment", 1);
l = g([
  K("components.weather.WeatherViewModel")
], l);
function m({ label: o, ariaLabel: e, value: t, disabled: i, onChange: r }) {
  const n = (s) => {
    const h = s.currentTarget.value;
    r?.(h ?? 0);
  };
  return d`<calcite-label .scale=${"m"}><calcite-slider .disabled=${i} .ariaLabel=${e} .labelText=${o} .value=${t} min=0 max=1 step=0.01 .scale=${"m"} @calciteSliderChange=${n} @calciteSliderInput=${n}></calcite-slider></calcite-label>`;
}
const J = D(l), Q = {
  sunny: "brightness",
  cloudy: "partly-cloudy",
  rainy: "rain",
  snowy: "snow",
  foggy: "fog"
}, X = 16, M = typeof navigator < "u" && navigator.userAgent.includes("Safari") && !navigator.userAgent.includes("Chrome") && !navigator.userAgent.includes("Chromium") && !navigator.userAgent.includes("Android") && !navigator.userAgent.includes("CriOS") && !navigator.userAgent.includes("FxiOS") && !navigator.userAgent.includes("Edg");
class Y extends E {
  constructor() {
    super(...arguments), this.viewModel = J(this), this.messages = V(), this.direction = B(), this._resizeAnimationFrame = void 0, this._weatherTypeSelector = void 0, this._resizeObserver = new ResizeObserver((e) => {
      for (const t of e)
        t.target instanceof HTMLElement && this._updateButtonScale(t.target, t.contentRect.width);
    }), this._buttonScale = "m", this._buttonRowMinHeight = void 0, this.view = this.viewModel.view, this.autoDestroyDisabled = !1, this.headingLevel = 4, this.hideHeader = !1, this.icon = "partly-cloudy", this.label = this.messages.componentLabel, this.state = this.viewModel.state, this.arcgisPropertyChange = R()("state"), this.arcgisReady = H();
  }
  static {
    this.properties = { _buttonScale: 16, _buttonRowMinHeight: 16, view: 0, autoDestroyDisabled: 5, headingLevel: 9, hideHeader: 5, icon: 1, label: 1, messageOverrides: 0, referenceElement: 1, state: 32 };
  }
  static {
    this.styles = N;
  }
  async destroy() {
    this._cleanupResizeObserver(), await this.manager.destroy();
  }
  _cleanupResizeObserver() {
    this._resizeObserver.disconnect(), this._weatherTypeSelector = void 0, this._buttonRowMinHeight = void 0, this._resizeAnimationFrame && (cancelAnimationFrame(this._resizeAnimationFrame), this._resizeAnimationFrame = void 0);
  }
  _makeWeatherTypeSelectorRef() {
    return (e) => {
      e !== this._weatherTypeSelector && (this._weatherTypeSelector && this._resizeObserver.unobserve(this._weatherTypeSelector), this._weatherTypeSelector = e, e && (this._resizeObserver.observe(e), this._updateButtonScale(e, e.clientWidth)));
    };
  }
  _updateButtonScale(e, t) {
    this._resizeAnimationFrame && cancelAnimationFrame(this._resizeAnimationFrame), this._resizeAnimationFrame = requestAnimationFrame(() => {
      const i = getComputedStyle(e), r = Number.parseFloat(i.getPropertyValue("--button-gap")), n = Number.parseFloat(i.getPropertyValue("--button-size-min")), a = Number.parseFloat(i.getPropertyValue("--button-size-s-max")), s = Number.parseFloat(i.getPropertyValue("--button-size-m-max")), h = (p.length - 1) * r, v = Math.max(n, Math.min((t - h) / p.length, s)), b = Math.floor(v);
      if (M) {
        const { _buttonRowMinHeight: c } = this, w = c === void 0 ? void 0 : c * p.length + h, y = w !== void 0 && c !== void 0 && c >= b && w <= t + X;
        this._buttonRowMinHeight = y ? c : b;
      } else
        this._buttonRowMinHeight = void 0;
      this._buttonScale = v < a ? "s" : v < s ? "m" : "l", this._resizeAnimationFrame = void 0;
    });
  }
  _getWarningMessage() {
    if (this.viewModel.error === "not-visible") {
      const { view: e } = this.viewModel;
      return e?.type === "3d" && e.environment.atmosphereEnabled ? this.messages.error?.notVisible : void 0;
    } else
      return;
  }
  _getErrorMessage() {
    const e = this.messages.error;
    if (e)
      switch (this.viewModel.error) {
        case "local-scene":
          return e.localScene;
        case "no-atmosphere":
          return e.noAtmosphere;
        case "unsupported-view":
          return e.unsupported;
        default:
          return;
      }
  }
  render() {
    return d`<div aria-label=${this.messages.componentLabelAccessible ?? _} class=${f({
      root: !0,
      [z.widget]: !0,
      [z.widgetDisabled]: this.viewModel.disabled
    })} role=region>${this._renderContent()}</div>`;
  }
  _renderContent() {
    const { headingLevel: e, messages: t, viewModel: i } = this, r = this._getWarningMessage(), n = i.current, a = i.state === "error";
    return d`${this.hideHeader ? void 0 : j({ level: e, children: t.componentLabel })}${a ? d`<div>${this._getErrorMessage()}</div>` : void 0}<div class=${f(L({ content: !0, error: a }))}><div .ariaLabel=${t.weatherType} class="weather-type-selector" role=radiogroup style=${P({
      "--weather-type-count": String(p.length),
      "--button-row-min-height": M ? `${this._buttonRowMinHeight ?? 0}px` : void 0
    })} ${F(this._makeWeatherTypeSelectorRef())}>${p.map(this._renderWeatherTypeButton, this)}</div>${Object.values(i.weatherByType).map((s) => d`<div class=${f({
      options: !0,
      hidden: s.type !== n?.type
    })}>${this._renderOptions(s)}</div>`)}${r ? this._renderWarning(r) : void 0}</div>`;
  }
  _renderWeatherTypeButton(e) {
    const t = this.messages[e]?.label ?? "", { disabled: i } = this.viewModel, r = !i && this.viewModel.current.type === e;
    return d`<calcite-button role=radio .label=${t} .name=${t} .appearance=${r ? "solid" : "outline"} .ariaChecked=${r ? "true" : "false"} tabindex=${(r ? void 0 : -1) ?? _} .disabled=${i} .iconStart=${Q[e]} .kind=${r ? "brand" : "neutral"} width=full .scale=${this._buttonScale} @keydown=${(n) => {
      const a = n.key;
      let s = ["ArrowRight", "ArrowDown"], h = ["ArrowLeft", "ArrowUp"];
      if (this.direction === "rtl") {
        const c = s;
        s = h, h = c;
      }
      const v = s.includes(a), b = h.includes(a);
      if (v || b) {
        n.preventDefault();
        const c = p.indexOf(e), w = p.length, x = (c + (b ? -1 : 1) + w) % w;
        this.viewModel.setWeatherByType(p[x]);
        const C = n.currentTarget.parentElement?.parentElement;
        C && Array.from(C.querySelectorAll("calcite-button") ?? [])[x]?.setFocus();
      }
    }} @click=${(n) => {
      if (r)
        return;
      this.viewModel.setWeatherByType(e);
      const a = n.currentTarget;
      requestAnimationFrame(() => {
        this.viewModel.current.type === e && a.setFocus();
      });
    }} title=${t ?? _}></calcite-button>`;
  }
  _renderOptions(e) {
    switch (e.type) {
      case "sunny":
        return this._renderSunnyOptions(e);
      case "cloudy":
        return this._renderCloudyOptions(e);
      case "rainy":
        return this._renderRainyOptions(e);
      case "snowy":
        return this._renderSnowyOptions(e);
      case "foggy":
        return this._renderFoggyOptions(e);
    }
  }
  _renderSunnyOptions(e) {
    const t = this.messages.sunny;
    return m({ disabled: this.viewModel.disabled, ariaLabel: t?.cloudCoverAria, label: t?.cloudCover, value: e.cloudCover, onChange: (i) => {
      e.cloudCover = i;
    } });
  }
  _renderCloudyOptions(e) {
    const t = this.messages.cloudy;
    return m({ disabled: this.viewModel.disabled, ariaLabel: t?.cloudCoverAria, label: t?.cloudCover, value: e.cloudCover, onChange: (i) => {
      e.cloudCover = i;
    } });
  }
  _renderRainyOptions(e) {
    const { disabled: t } = this.viewModel, i = this.messages.rainy;
    return d`${m({ disabled: t, ariaLabel: i?.cloudCoverAria, label: i?.cloudCover, value: e.cloudCover, onChange: (r) => {
      e.cloudCover = r;
    } })}${m({ disabled: t, ariaLabel: i?.precipitationAria, label: i?.precipitation, value: e.precipitation, onChange: (r) => {
      e.precipitation = r;
    } })}`;
  }
  _renderSnowyOptions(e) {
    const { disabled: t } = this.viewModel, i = this.messages.snowy;
    return d`${m({ disabled: t, ariaLabel: i?.cloudCoverAria, label: i?.cloudCover, value: e.cloudCover, onChange: (r) => {
      e.cloudCover = r;
    } })}${m({ disabled: t, ariaLabel: i?.precipitationAria, label: i?.precipitation, value: e.precipitation, onChange: (r) => {
      e.precipitation = r;
    } })}<calcite-label layout=inline title=${i?.snowCoverTooltip ?? _}><calcite-checkbox .checked=${e.snowCover === "enabled"} .disabled=${t} @calciteCheckboxChange=${(r) => {
      this.viewModel.setSnowCoverEnabled(r.currentTarget.checked);
    }}></calcite-checkbox>${i?.snowCover}</calcite-label>`;
  }
  _renderFoggyOptions(e) {
    const t = this.messages.foggy;
    return m({ disabled: this.viewModel.disabled, ariaLabel: t?.fogStrengthAria, label: t?.fogStrength, value: e.fogStrength, onChange: (i) => {
      e.fogStrength = i;
    } });
  }
  _renderWarning(e) {
    return d`<calcite-notice aria-live=polite open icon=exclamation-mark-triangle-f kind=warning><div slot=title class="warning-title">${this.messages.warningTitle}</div><div slot=message>${e}</div></calcite-notice>`;
  }
}
O("arcgis-weather", Y);
export {
  Y as ArcgisWeather
};
