/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as customElement } from "../../chunks/runtime.js";
import { css, html } from "lit";
import { createRef, ref } from "lit-html/directives/ref.js";
import { LitElement } from "@arcgis/lumina";
import Legend from "@arcgis/core/widgets/Legend";
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.
 */

/* TODO update all of these :host */
:host {
  display: block;
}`;
class MapLegend extends LitElement {
  constructor() {
    super(...arguments);
    this._legendElement = createRef();
  }
  static {
    this.properties = { mapView: [0, {}, { attribute: false }], legendWidget: [0, {}, { attribute: false }] };
  }
  static {
    this.styles = styles;
  }
  willUpdate(changes) {
    if (changes.has("mapView")) {
      void this.mapViewWatchHandler();
    }
  }
  async mapViewWatchHandler() {
    await this.mapView?.when(() => {
      this.mapView && this._initLegend(this.mapView);
    });
  }
  render() {
    return html`<div ${ref(this._legendElement)}></div>`;
  }
  async loaded() {
    if (this.mapView) {
      await this.mapViewWatchHandler();
    }
  }
  _initLegend(view) {
    if (view) {
      if (!this.legendWidget) {
        this.legendWidget = new Legend({
          container: this._legendElement.value,
          respectLayerDefinitionExpression: true,
          view
        });
      } else {
        this.legendWidget.view = view;
      }
    }
  }
}
customElement("map-legend", MapLegend);
export {
  MapLegend
};
