import { unsafeCSS, LitElement, nothing, html } from "lit";
import { property, state } from "lit/decorators.js";
import compentStyle from "./dropdown-button.css.js";
import "../../icons/icon-drop-down-google.js";
import "../button/button.js";
import { customElement } from "../../decorator.js";
import { classMap } from "lit/directives/class-map.js";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result) __defProp(target, key, result);
  return result;
};
var DropdownButtonType = /* @__PURE__ */ ((DropdownButtonType2) => {
  DropdownButtonType2["label"] = "label";
  DropdownButtonType2["icon"] = "icon";
  DropdownButtonType2["labelIcon"] = "label-icon";
  return DropdownButtonType2;
})(DropdownButtonType || {});
let ObcDropdownButton = class extends LitElement {
  constructor() {
    super(...arguments);
    this.options = [];
    this.disabled = false;
    this.fullWidth = false;
    this.type = "label";
    this.openTop = false;
    this.integration = false;
    this.selectedValue = "";
    this.selectedLabel = "";
  }
  connectedCallback() {
    super.connectedCallback();
    this.updateSelectedValues();
  }
  willUpdate(changedProperties) {
    if (changedProperties.has("value") || changedProperties.has("options")) {
      this.updateSelectedValues();
    }
  }
  updateSelectedValues() {
    if (this.options.length === 0) {
      this.selectedValue = "";
      this.selectedLabel = "";
      return;
    }
    this.selectedValue = this.value || this.options[0].value;
    this.selectedLabel = this.value ? this.options.find((item) => item.value === this.value)?.label || "" : this.options[0].label;
  }
  render() {
    return html`
      <div
        class=${classMap({
      wrapper: true,
      "full-width": this.fullWidth,
      "open-top": this.openTop,
      integration: this.integration,
      disabled: this.disabled
    })}
      >
        <div class="visible-wrapper">
          ${this.type !== "label" ? html`<div class="icon-container"><slot name="icon"></slot></div>` : nothing}
          ${this.type !== "icon" ? html`<div class="label">${this.selectedLabel}</div>` : nothing}
          <div class="icon">
            <obi-drop-down-google></obi-drop-down-google>
          </div>
        </div>
        <select @change=${this.changeHandler} ?disabled=${this.disabled}>
          ${this.options.map((item) => {
      const indent = item.level ? (item.level - 1) * 2 : 0;
      const indentText = [];
      for (let i = 0; i < indent; i++) {
        indentText.push(html`&nbsp;`);
      }
      return html`<option
              value=${item.value}
              ?selected=${item.value === this.selectedValue}
            >
              ${indentText}${item.label}
            </option>`;
    })}
        </select>
      </div>
    `;
  }
  /**
   * Handles the dropdown-change and change event when a new option is selected. Updates the selected value and label, and dispatches a `dropdown-change` and 'change' event with the new selection.
   *
   * @fires dropdown-change {ObcDropdownButtonChangeEvent} - Fired when the user selects a different option.
   * @fires change {ObcDropdownButtonChangeEvent} - Fired when the user selects a different option.
   */
  changeHandler(event) {
    const target = event.target;
    this.selectedValue = target.value;
    this.selectedLabel = this.options.find((item) => item.value === this.selectedValue).label.trim();
    this.dispatchEvent(
      new CustomEvent("dropdown-change", {
        detail: { value: this.selectedValue, label: this.selectedLabel }
      })
    );
    this.dispatchEvent(
      new CustomEvent("change", {
        detail: { value: this.selectedValue, label: this.selectedLabel }
      })
    );
  }
};
ObcDropdownButton.styles = unsafeCSS(compentStyle);
__decorateClass([
  property({ type: Array })
], ObcDropdownButton.prototype, "options", 2);
__decorateClass([
  property({ type: String })
], ObcDropdownButton.prototype, "value", 2);
__decorateClass([
  property({ type: Boolean })
], ObcDropdownButton.prototype, "disabled", 2);
__decorateClass([
  property({ type: Boolean })
], ObcDropdownButton.prototype, "fullWidth", 2);
__decorateClass([
  property({ type: String })
], ObcDropdownButton.prototype, "type", 2);
__decorateClass([
  property({ type: Boolean })
], ObcDropdownButton.prototype, "openTop", 2);
__decorateClass([
  property({ type: Boolean })
], ObcDropdownButton.prototype, "integration", 2);
__decorateClass([
  state()
], ObcDropdownButton.prototype, "selectedValue", 2);
__decorateClass([
  state()
], ObcDropdownButton.prototype, "selectedLabel", 2);
ObcDropdownButton = __decorateClass([
  customElement("obc-dropdown-button")
], ObcDropdownButton);
export {
  DropdownButtonType,
  ObcDropdownButton
};
//# sourceMappingURL=dropdown-button.js.map
