import { html, nothing } from 'lit';
import { query, state, property } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { boolean } from '../../internal/boolean.mjs';
import { FlowRdfFormControl } from '../../internal/flow-ui-rdf-element.mjs';
import { FormControlController } from '../../internal/form.mjs';
import { HasSlotController } from '../../internal/slot.mjs';
import { watch } from '../../internal/watch.mjs';
import styles from './base-control.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 FlowUiBaseControl extends FlowRdfFormControl {
  constructor() {
    super(...arguments);
    this.formControlController = new FormControlController(this, {
      assumeInteractionOn: ["flow-ui-blur", "flow-ui-input"]
    });
    this.hasSlotController = new HasSlotController(this, "help-text", "label");
    this.hasFocus = false;
    this.size = "medium";
    this.filled = false;
    this.pill = false;
    this.label = "";
    this.helpText = "";
    this.disabled = false;
    this.form = "";
    this.required = false;
    this.inline = false;
    this.divider = false;
  }
  connectedCallback() {
    super.connectedCallback();
    this.disabled = boolean(this.disabled, true);
    this.required = boolean(this.required, true);
  }
  /** Gets the validity state object */
  get validity() {
    return this.input.validity;
  }
  /** Gets the validation message */
  get validationMessage() {
    return this.input.validationMessage;
  }
  firstUpdated() {
    this.formControlController.updateValidity();
  }
  handleBlur() {
    this.hasFocus = false;
    this.emit("flow-ui-blur");
  }
  handleFocus() {
    this.hasFocus = true;
    this.emit("flow-ui-focus");
  }
  handleDisabledChange() {
    this.formControlController.setValidity(this.disabled);
  }
  /** Sets focus on the input. */
  focus(options) {
    this.input.focus(options);
  }
  /** Removes focus from the input. */
  blur() {
    this.input.blur();
  }
  /** Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid. */
  checkValidity() {
    return this.input.checkValidity();
  }
  /** Gets the associated form, if one exists. */
  getForm() {
    return this.formControlController.getForm();
  }
  /** Checks for validity and shows the browser's validation message if the control is invalid. */
  reportValidity() {
    return this.input.reportValidity();
  }
  /** Sets a custom validation message. Pass an empty string to restore validity. */
  setCustomValidity(message) {
    this.input.setCustomValidity(message);
    this.formControlController.updateValidity();
  }
  render() {
    const hasLabelSlot = this.hasSlotController.test("label");
    const hasHelpTextSlot = this.hasSlotController.test("help-text");
    const hasLabel = this.label ? true : !!hasLabelSlot;
    const hasHelpText = this.helpText ? true : !!hasHelpTextSlot;
    return html`
      <div
        part="form-control"
        class=${classMap({
      "form-control": true,
      "form-control--small": this.size === "small",
      "form-control--medium": this.size === "medium",
      "form-control--large": this.size === "large",
      "form-control--inline": this.inline,
      "form-control--disabled": this.disabled,
      "form-control--focused": this.hasFocus,
      "form-control--has-label": hasLabel,
      "form-control--has-help-text": hasHelpText,
      "form-control--has-divider": this.divider,
      "form-control--filled": this.filled,
      "form-control--pill": this.pill
    })}
      >
        <label
          part="form-control-label"
          class="form-control__label"
          for="input"
          aria-hidden=${hasLabel ? "false" : "true"}
        >
          <slot name="label">${this.label}</slot>
        </label>

        <slot
          name="help-text"
          part="form-control-help-text"
          id="help-text"
          class="form-control__help-text"
          aria-hidden=${hasHelpText ? "false" : "true"}
        >
          ${this.helpText}
        </slot>

        <div part="form-control-input" class=${classMap({
      "form-control__input": true
    })}>
            <input class="input__control" placeholder="Enter text" ?disabled=${this.disabled} ?required=${this.required} @focus=${this.handleFocus} @blur=${this.handleBlur}></input>
        </div>
        ${this.divider ? this.renderDivider() : nothing}
        </div>

      </div>
    `;
  }
}
FlowUiBaseControl.styles = styles;
__decorateClass([
  query(".input__control")
], FlowUiBaseControl.prototype, "input", 2);
__decorateClass([
  state()
], FlowUiBaseControl.prototype, "hasFocus", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiBaseControl.prototype, "size", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiBaseControl.prototype, "filled", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiBaseControl.prototype, "pill", 2);
__decorateClass([
  property()
], FlowUiBaseControl.prototype, "label", 2);
__decorateClass([
  property({ attribute: "help-text" })
], FlowUiBaseControl.prototype, "helpText", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiBaseControl.prototype, "disabled", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiBaseControl.prototype, "form", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiBaseControl.prototype, "required", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiBaseControl.prototype, "inline", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiBaseControl.prototype, "divider", 2);
__decorateClass([
  watch("disabled", { waitUntilFirstUpdate: true })
], FlowUiBaseControl.prototype, "handleDisabledChange", 1);

export { FlowUiBaseControl as default };
