import { query, state, property } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { html } from 'lit/static-html.js';
import { boolean } from '../../internal/boolean.mjs';
import FlowRdfElement from '../../internal/flow-ui-rdf-element.mjs';
import { HasSlotController } from '../../internal/slot.mjs';
import { watch } from '../../internal/watch.mjs';
import styles from './tool-button.styles.mjs';

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;
};
class FlowUiToolButton extends FlowRdfElement {
  constructor() {
    super(...arguments);
    this.hasSlotController = new HasSlotController(this, "[default]", "prefix", "suffix", "icon");
    this.hasFocus = false;
    this.checked = false;
    this.disabled = false;
    this.size = "medium";
    this.pill = false;
    this.outline = false;
    this.tooltip = "";
    this.tooltipPlacement = "top";
    this.tooltipDistance = 8;
    this.tooltipSkidding = 0;
    this.iconLibrary = "default";
  }
  connectedCallback() {
    super.connectedCallback();
    this.checked = boolean(this.checked, true);
    this.disabled = boolean(this.disabled, true);
    this.pill = boolean(this.pill, true);
    this.outline = boolean(this.outline, true);
    this.setAttribute("role", "presentation");
  }
  handleBlur() {
    this.hasFocus = false;
    this.emit("flow-ui-blur");
  }
  handleClick(e) {
    if (this.disabled) {
      e.preventDefault();
      e.stopPropagation();
      return;
    }
  }
  handleFocus() {
    this.hasFocus = true;
    this.emit("flow-ui-focus");
  }
  handleDisabledChange() {
    this.setAttribute("aria-disabled", this.disabled ? "true" : "false");
  }
  /** Sets focus on the radio button. */
  focus(options) {
    this.input.focus(options);
  }
  /** Removes focus from the radio button. */
  blur() {
    this.input.blur();
  }
  renderWithTooltip(content) {
    return html`
      <flow-ui-tooltip
        part="tooltip"
        class=${classMap({
      "tool-button__tooltip": true
    })}
        hoist
        placement=${ifDefined(this.tooltipPlacement)}
        distance=${ifDefined(this.tooltipDistance)}
        skidding=${ifDefined(this.tooltipSkidding)}
      >
        ${content}
        <slot name="tooltip" slot="content">${this.tooltip}</slot>
      </flow-ui-tooltip>
    `;
  }
  render() {
    const hasTooltip = this.tooltip || this.hasSlotController.test("tooltip");
    const toolButtonTemplate = html`
      <div part="base" role="presentation">
        <button
          part="${`button${this.checked ? " button--checked" : ""}`}"
          role="radio"
          aria-checked="${this.checked}"
          class=${classMap({
      button: true,
      "button--default": true,
      "button--x-small": this.size === "x-small",
      "button--small": this.size === "small",
      "button--medium": this.size === "medium",
      "button--large": this.size === "large",
      "button--checked": this.checked,
      "button--disabled": this.disabled,
      "button--focused": this.hasFocus,
      "button--outline": this.outline,
      "button--text": !this.outline,
      "button--pill": this.pill,
      "button--has-label": this.hasSlotController.test("[default]"),
      "button--has-prefix": this.hasSlotController.test("prefix"),
      "button--has-suffix": this.hasSlotController.test("suffix"),
      "button--has-icon": this.icon || this.hasSlotController.test("icon"),
      "button--primary": this.variant === "primary",
      "button--secondary": this.variant === "secondary",
      "button--success": this.variant === "success",
      "button--warning": this.variant === "warning",
      "button--danger": this.variant === "danger",
      "button--neutral": this.variant === "neutral"
    })}
          aria-disabled=${this.disabled}
          type="button"
          value=${ifDefined(this.value)}
          tabindex="0"
          @blur=${this.handleBlur}
          @focus=${this.handleFocus}
          @click=${this.handleClick}
        >
          <slot name="prefix" part="prefix" class="button__prefix"></slot>
          <div part="label-wrapper" class="button__label-wrapper">
            <slot name="icon" part="icon" class="button__icon">
              <flow-ui-icon
                name=${ifDefined(this.icon)}
                src=${ifDefined(this.iconSrc)}
                library=${ifDefined(this.iconLibrary)}
              ></flow-ui-icon>
            </slot>

            <span part="label" class="button__label"><slot part="label"></slot></span>
          </div>
          <slot name="suffix" part="suffix" class="button__suffix"></slot>
        </button>
      </div>
    `;
    if (hasTooltip) {
      return this.renderWithTooltip(toolButtonTemplate);
    } else {
      return toolButtonTemplate;
    }
  }
}
FlowUiToolButton.styles = styles;
__decorateClass([
  query(".button")
], FlowUiToolButton.prototype, "input", 2);
__decorateClass([
  query(".hidden-input")
], FlowUiToolButton.prototype, "hiddenInput", 2);
__decorateClass([
  state()
], FlowUiToolButton.prototype, "hasFocus", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiToolButton.prototype, "checked", 2);
__decorateClass([
  property()
], FlowUiToolButton.prototype, "value", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiToolButton.prototype, "disabled", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiToolButton.prototype, "size", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiToolButton.prototype, "pill", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiToolButton.prototype, "outline", 2);
__decorateClass([
  property()
], FlowUiToolButton.prototype, "tooltip", 2);
__decorateClass([
  property({ attribute: "tooltip-placement" })
], FlowUiToolButton.prototype, "tooltipPlacement", 2);
__decorateClass([
  property({ attribute: "tooltip-distance", type: Number })
], FlowUiToolButton.prototype, "tooltipDistance", 2);
__decorateClass([
  property({ attribute: "tooltip-skidding", type: Number })
], FlowUiToolButton.prototype, "tooltipSkidding", 2);
__decorateClass([
  property()
], FlowUiToolButton.prototype, "icon", 2);
__decorateClass([
  property({ attribute: "icon-src" })
], FlowUiToolButton.prototype, "iconSrc", 2);
__decorateClass([
  property({ attribute: "icon-library" })
], FlowUiToolButton.prototype, "iconLibrary", 2);
__decorateClass([
  property()
], FlowUiToolButton.prototype, "variant", 2);
__decorateClass([
  watch("disabled", { waitUntilFirstUpdate: true })
], FlowUiToolButton.prototype, "handleDisabledChange", 1);

export { FlowUiToolButton as default };
