import { unsafeCSS, LitElement, html, nothing } from "lit";
import { property, queryAssignedElements } from "lit/decorators.js";
import compentStyle from "./integration-dropdown-button.css.js";
import "../../icons/icon-drop-down-google.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;
};
let ObcIntegrationDropdownButton = class extends LitElement {
  constructor() {
    super(...arguments);
    this.options = [];
    this.hasFleet = false;
    this.fleetLabel = "";
    this.placeholder = "";
    this.disabled = false;
    this.fullWidth = false;
    this.openTop = false;
  }
  get selectedItem() {
    if (this.options.length === 0) {
      return void 0;
    }
    return this.options.find((item) => item.value === this.value);
  }
  get fleetSelected() {
    return this.value === "fleet";
  }
  updateSelectedFleetStatus() {
    this.fleetItems.forEach((item) => {
      const button = item;
      button.selected = this.fleetSelected;
    });
  }
  updated(_changedProperties) {
    super.updated(_changedProperties);
    this.updateSelectedFleetStatus();
  }
  get renderButtonLabel() {
    if (this.fleetSelected) {
      return html`<div class="label">${this.fleetLabel}</div>`;
    }
    if (this.selectedItem) {
      return html`<div class="icon-container">${this.selectedItem.icon}</div>
        <div class="label">${this.selectedItem.label}</div>`;
    }
    return html`<div class="label placeholder">${this.placeholder}</div>`;
  }
  render() {
    return html`
      <div
        class=${classMap({
      wrapper: true,
      "full-width": this.fullWidth,
      "open-top": this.openTop,
      disabled: this.disabled
    })}
      >
        <div class="visible-wrapper">
          ${this.renderButtonLabel}
          <div class="icon">
            <obi-drop-down-google></obi-drop-down-google>
          </div>
        </div>
        <select @change=${this.changeHandler} ?disabled=${this.disabled}>
          ${this.hasFleet ? html`
                <option
                  value="fleet"
                  class="fleet-option"
                  ?selected=${this.fleetSelected}
                >
                  <slot name="fleet"></slot>
                </option>
              ` : nothing}
          ${this.options.map((item) => {
      return html`<option
              value=${item.value}
              ?selected=${item.value === this.value}
              ?disabled=${item.disabled}
            >
              <div class="icon">${item.icon}</div>
              <div class="text-container">
                <div class="label">${item.label}</div>
                ${item.status ? html`<div class="status">${item.status}</div>` : nothing}
              </div>
            </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 {ObcIntegrationDropdownButtonChangeEvent} - Fired when the user selects a different option.
   * @fires change {ObcIntegrationDropdownButtonChangeEvent} - Fired when the user selects a different option.
   */
  changeHandler(event) {
    const target = event.target;
    this.value = target.value;
    this.dispatchEvent(
      new CustomEvent("change", {
        detail: {
          value: this.value,
          label: this.value === "fleet" ? this.fleetLabel : this.selectedItem?.label
        }
      })
    );
  }
};
ObcIntegrationDropdownButton.styles = unsafeCSS(compentStyle);
__decorateClass([
  property({ type: Array })
], ObcIntegrationDropdownButton.prototype, "options", 2);
__decorateClass([
  property({ type: Boolean })
], ObcIntegrationDropdownButton.prototype, "hasFleet", 2);
__decorateClass([
  property({ type: String })
], ObcIntegrationDropdownButton.prototype, "fleetLabel", 2);
__decorateClass([
  property({ type: String })
], ObcIntegrationDropdownButton.prototype, "value", 2);
__decorateClass([
  property({ type: String })
], ObcIntegrationDropdownButton.prototype, "placeholder", 2);
__decorateClass([
  property({ type: Boolean })
], ObcIntegrationDropdownButton.prototype, "disabled", 2);
__decorateClass([
  property({ type: Boolean })
], ObcIntegrationDropdownButton.prototype, "fullWidth", 2);
__decorateClass([
  property({ type: Boolean })
], ObcIntegrationDropdownButton.prototype, "openTop", 2);
__decorateClass([
  queryAssignedElements({ slot: "fleet" })
], ObcIntegrationDropdownButton.prototype, "fleetItems", 2);
ObcIntegrationDropdownButton = __decorateClass([
  customElement("obc-integration-dropdown-button")
], ObcIntegrationDropdownButton);
export {
  ObcIntegrationDropdownButton
};
//# sourceMappingURL=integration-dropdown-button.js.map
