import { html } from 'lit';
import { query, property } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { boolean } from '../../internal/boolean.mjs';
import FlowRdfElement, { FlowRdfFormControl } from '../../internal/flow-ui-rdf-element.mjs';
import { HasSlotController } from '../../internal/slot.mjs';
import { watch } from '../../internal/watch.mjs';
import styles from './form-group.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 FlowUiFormGroup extends FlowRdfElement {
  constructor() {
    super(...arguments);
    this.hasSlotController = new HasSlotController(this, "help-text", "label");
    this.label = "";
    this.size = "medium";
    this.helpText = "";
    this.borders = false;
    this.variant = "default";
  }
  connectedCallback() {
    super.connectedCallback();
    this.classList.add(FlowRdfFormControl.FLOW_UI_FORM_CONTROL_CLASS);
    this.borders = boolean(this.borders, true);
  }
  handleClassChange() {
    this.classList.add(FlowRdfFormControl.FLOW_UI_FORM_CONTROL_CLASS);
  }
  handleStyleChange() {
    this.syncControls();
  }
  syncControls() {
    if (customElements.get("flow-ui-form-group")) {
      const controls = this.getAllControls();
      controls.forEach((control, i) => {
        control.size = this.size;
        control.divider = this.borders && i < controls.length - 1;
      });
    } else {
      customElements.whenDefined("flow-ui-form-group").then(() => this.syncControls());
    }
  }
  getAllControls() {
    return [...this.querySelectorAll(".flow-ui-form-control")];
  }
  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;
    const defaultSlot = html`
      <slot @slotchange=${this.syncControls}></slot>
    `;
    return html`
      <div
        part="form-control"
        class=${classMap({
      "form-control": true,
      "form-control--has-label": hasLabel,
      "form-control--has-help-text": hasHelpText,
      "form-control--small": this.size === "small",
      "form-control--medium": this.size === "medium",
      "form-control--large": this.size === "large",
      "form-control--default": this.variant === "default",
      "form-control--warning": this.variant === "warning",
      "form-control--danger": this.variant === "danger",
      "form-group--has-dividers": this.borders,
      "form-group--has-border": this.borders
    })}
        aria-labelledby="label"
        aria-describedby="help-text"
        aria-errormessage="error-message"
      >
        <slot
          name="label"
          part="label"
          id="label"
          class="form-control__label"
          aria-hidden=${hasLabel ? "false" : "true"}
        >
          ${this.label}
        </slot>

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

        <div part="control" class="form-group__content">${defaultSlot}</div>
      </div>
    `;
  }
}
FlowUiFormGroup.styles = styles;
__decorateClass([
  query("slot:not([name])")
], FlowUiFormGroup.prototype, "defaultSlot", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiFormGroup.prototype, "class", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiFormGroup.prototype, "label", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiFormGroup.prototype, "size", 2);
__decorateClass([
  property({ attribute: "help-text" })
], FlowUiFormGroup.prototype, "helpText", 2);
__decorateClass([
  property({ type: Boolean, attribute: "borders", reflect: true })
], FlowUiFormGroup.prototype, "borders", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiFormGroup.prototype, "variant", 2);
__decorateClass([
  watch("class")
], FlowUiFormGroup.prototype, "handleClassChange", 1);
__decorateClass([
  watch(["size", "noDividers", "noBorder"], { waitUntilFirstUpdate: true })
], FlowUiFormGroup.prototype, "handleStyleChange", 1);

export { FlowUiFormGroup as default };
