import {
  defaultValue
} from "./chunk.GI7VDIWX.js";
import {
  FormControlController
} from "./chunk.C3WIBDGG.js";
import {
  HasSlotController
} from "./chunk.IVMQWO2L.js";
import {
  watch
} from "./chunk.X2OQMWGY.js";
import {
  SolidElement,
  customElement
} from "./chunk.H53WQBDK.js";
import {
  __decorateClass
} from "./chunk.S3NI7NKU.js";

// src/components/textarea/textarea.ts
import { css, html } from "lit";
import { ifDefined } from "lit/directives/if-defined.js";
import { live } from "lit/directives/live.js";
import { property, query, state } from "lit/decorators.js";
import cx from "classix";
var SdTextarea = class extends SolidElement {
  constructor() {
    super(...arguments);
    this.formControlController = new FormControlController(this);
    this.hasSlotController = new HasSlotController(this, "help-text", "label");
    this.hasFocus = false;
    this.showValidStyle = false;
    this.showInvalidStyle = false;
    this.title = "";
    this.name = "";
    this.value = "";
    this.size = "lg";
    this.label = "";
    this.helpText = "";
    this.placeholder = "";
    this.rows = 4;
    this.disabled = false;
    this.visuallyDisabled = false;
    this.readonly = false;
    this.form = "";
    this.required = false;
    this.floatingLabel = false;
    this.styleOnValid = false;
    this.spellcheck = true;
    this.defaultValue = "";
  }
  /** Gets the validity state object */
  get validity() {
    return this.textarea.validity;
  }
  /** Gets the validation message */
  get validationMessage() {
    return this.textarea.validationMessage;
  }
  connectedCallback() {
    super.connectedCallback();
    this.updateComplete.then(() => {
      this.setTextareaHeight();
    });
  }
  firstUpdated() {
    this.formControlController.updateValidity();
  }
  disconnectedCallback() {
    super.disconnectedCallback();
  }
  handleBlur() {
    this.hasFocus = false;
    this.emit("sd-blur");
  }
  handleChange() {
    this.value = this.textarea.value;
    this.setTextareaHeight();
    this.emit("sd-change");
  }
  handleFocus() {
    this.hasFocus = true;
    this.emit("sd-focus");
  }
  handleInput() {
    if (this.visuallyDisabled) {
      this.textarea.value = this.value;
      return;
    }
    this.value = this.textarea.value;
    this.formControlController.updateValidity();
    this.emit("sd-input");
  }
  handleInvalid(event) {
    this.formControlController.setValidity(false);
    this.formControlController.emitInvalidEvent(event);
    this.invalidMessage.textContent = event.target.validationMessage;
  }
  setTextareaHeight() {
    this.textarea.style.height = void 0;
  }
  handleDisabledChange() {
    this.formControlController.setValidity(this.disabled);
  }
  handleRowsChange() {
    this.setTextareaHeight();
  }
  async handleValueChange() {
    await this.updateComplete;
    this.formControlController.updateValidity();
    this.setTextareaHeight();
  }
  handleSizeChange() {
    this.size = this.floatingLabel && this.size === "sm" ? "md" : this.size;
  }
  /** Sets focus on the textarea. */
  focus(options) {
    this.textarea.focus(options);
  }
  /** Removes focus from the textarea. */
  blur() {
    this.textarea.blur();
  }
  /** Selects all the text in the textarea. */
  select() {
    this.textarea.select();
  }
  /** Gets or sets the textarea's scroll position. */
  scrollPosition(position) {
    if (position) {
      if (typeof position.top === "number") this.textarea.scrollTop = position.top;
      if (typeof position.left === "number") this.textarea.scrollLeft = position.left;
      return void 0;
    }
    return {
      top: this.textarea.scrollTop,
      left: this.textarea.scrollTop
    };
  }
  /** Sets the start and end positions of the text selection (0-based). */
  setSelectionRange(selectionStart, selectionEnd, selectionDirection = "none") {
    this.textarea.setSelectionRange(selectionStart, selectionEnd, selectionDirection);
  }
  /** Replaces a range of text with a new string. */
  setRangeText(replacement, start, end, selectMode) {
    this.textarea.setRangeText(replacement, start, end, selectMode);
    if (this.value !== this.textarea.value) {
      this.value = this.textarea.value;
    }
    if (this.value !== this.textarea.value) {
      this.value = this.textarea.value;
      this.setTextareaHeight();
    }
  }
  /** Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid. */
  checkValidity() {
    var _a;
    return (_a = this.textarea) == null ? void 0 : _a.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() {
    this.formControlController.fakeUserInteraction();
    return this.textarea.reportValidity();
  }
  /** Sets a custom validation message. Pass an empty string to restore validity. */
  setCustomValidity(message) {
    this.textarea.setCustomValidity(message);
    this.formControlController.updateValidity();
  }
  render() {
    const slots = {
      label: this.hasSlotController.test("label"),
      helpText: this.hasSlotController.test("help-text"),
      tooltip: this.hasSlotController.test("tooltip")
    };
    const hasLabel = this.label ? true : !!slots["label"];
    const hasHelpText = this.helpText ? true : !!slots["helpText"];
    const hasTooltip = !!slots["tooltip"];
    const hasValue = this.value !== null && String(this.value).length > 0;
    const isFloatingLabelActive = this.floatingLabel && hasLabel && (this.hasFocus && !this.visuallyDisabled || hasValue);
    const textareaState = this.disabled ? "disabled" : this.visuallyDisabled && !this.hasFocus ? "visuallyDisabled" : this.readonly ? "readonly" : this.hasFocus && this.showInvalidStyle ? "activeInvalid" : this.hasFocus && this.styleOnValid && this.showValidStyle ? "activeValid" : this.hasFocus ? "active" : this.showInvalidStyle ? "invalid" : this.styleOnValid && this.showValidStyle ? "valid" : "default";
    const textSize = this.size === "sm" ? "text-sm" : "text-base";
    const iconSize = {
      sm: "text-base top-1",
      md: "text-lg top-2",
      lg: "text-xl top-2"
    }[this.size];
    return html`<div part="form-control" class="flex flex-col h-full text-left">${hasLabel && !this.floatingLabel || hasTooltip ? html`<div class="flex items-center gap-1 mb-2"><label part="form-control-label" id="label" class="${cx(
      hasLabel ? "inline-block" : "hidden",
      textSize,
      this.visuallyDisabled || this.disabled ? "text-neutral-500" : " form-control-color-text"
    )}" for="input" aria-hidden="${hasLabel ? "false" : "true"}"><slot name="label">${this.label}</slot></label> ${slots["tooltip"] ? html`<slot name="tooltip"></slot>` : ""}</div>` : null}<div part="form-control-input" class="${cx("relative h-full w-full", this.disabled && "cursor-not-allowed")}">${hasLabel && this.floatingLabel ? html`<label id="label" part="form-control-floating-label" class="${cx(
      "absolute left-4 z-20 transition-all duration-200 cursor-text",
      textSize,
      !isFloatingLabelActive ? "top-2.5 form-control-color-text" : "top-2 text-xs"
    )}" for="input"><span class="${cx(
      "leading-none",
      (this.visuallyDisabled || this.disabled) && "text-neutral-500",
      isFloatingLabelActive && !this.visuallyDisabled && !this.disabled && "form-control--filled__floating-label-color-text"
    )}">${this.label}</span></label>` : null}<div part="border" class="${cx(
      "absolute w-full h-full pointer-events-none border form-control-border-radius transition-[border] duration-medium ease-in-out",
      {
        disabled: "border-neutral-500",
        visuallyDisabled: "border-neutral-500",
        readonly: "form-control-color-border",
        activeInvalid: "border-error border-2",
        activeValid: "border-success border-2",
        active: "border-primary border-2",
        invalid: "border-error",
        valid: "border-success",
        default: "form-control-color-border"
      }[textareaState]
    )}"></div><div part="base" class="${cx(
      "textarea h-full flex items-top form-control-border-radius group transition-colors duration-medium hover:duration-fast ease-in-out",
      this.floatingLabel && "pt-6",
      {
        sm: "textarea-sm",
        md: "textarea-md",
        lg: "textarea-lg"
      }[this.size],
      !this.disabled && !this.readonly && !this.visuallyDisabled ? "hover:[@media(hover:hover)]:form-control--hover-color-background" : "",
      this.readonly ? "form-control--read-only-color-background" : "bg-white",
      textareaState === "disabled" || textareaState === "visuallyDisabled" ? "text-neutral-500" : "text-black",
      ["invalid", "activeInvalid"].includes(textareaState) && "form-control--invalid-color-background"
    )}"><textarea part="textarea" id="input" class="${cx(
      "ps-4 flex-grow focus:outline-none bg-transparent placeholder:text-neutral-700 resize-none group-has-[sd-6-22-0-icon]:pe-8",
      {
        sm: isFloatingLabelActive ? "pb-1" : "py-2",
        md: isFloatingLabelActive ? "pb-1" : "py-2",
        lg: isFloatingLabelActive ? "pb-2" : "py-2"
      }[this.size],
      this.disabled && "cursor-not-allowed",
      textSize
    )}" title="${this.title}" name="${ifDefined(this.name)}" .value="${live(this.value)}" ?disabled="${this.disabled}" ?readonly="${this.readonly}" ?required="${this.required}" ?visually-disabled="${this.visuallyDisabled}" placeholder="${!this.floatingLabel || isFloatingLabelActive ? ifDefined(this.placeholder) : ""}" minlength="${ifDefined(this.minlength)}" maxlength="${ifDefined(this.maxlength)}" rows="${ifDefined(this.rows)}" autocapitalize="${ifDefined(this.autocapitalize)}" autocorrect="${ifDefined(this.autocorrect)}" ?autofocus="${this.autofocus}" spellcheck="${ifDefined(this.spellcheck)}" enterkeyhint="${ifDefined(this.enterkeyhint)}" inputmode="${ifDefined(this.inputmode)}" aria-describedby="help-text invalid-message" aria-invalid="${this.showInvalidStyle}" aria-disabled="${this.disabled || this.visuallyDisabled}" @change="${this.handleChange}" @input="${this.handleInput}" @invalid="${this.handleInvalid}" @focus="${this.handleFocus}" @blur="${this.handleBlur}"></textarea> ${this.showInvalidStyle ? html`<sd-6-22-0-icon class="${cx("text-error absolute right-4 pointer-events-none", iconSize)}" library="_internal" name="risk" part="invalid-icon"></sd-6-22-0-icon>` : ""} ${this.styleOnValid && this.showValidStyle ? html`<sd-6-22-0-icon class="${cx("text-success absolute right-4 pointer-events-none", iconSize)}" library="_internal" name="confirm-circle" part="valid-icon"></sd-6-22-0-icon>` : ""}</div></div><slot name="help-text" part="form-control-help-text" id="help-text" class="${cx(
      "text-sm mt-1",
      hasHelpText && !this.showInvalidStyle ? "block" : "hidden",
      this.visuallyDisabled || this.disabled ? "text-neutral-500" : "text-neutral-700"
    )}" aria-hidden="${!hasHelpText || this.showInvalidStyle}">${this.helpText}</slot></div>${this.formControlController.renderInvalidMessage(this.size)}`;
  }
};
/**
 * Inherits global stylesheet including TailwindCSS
 */
SdTextarea.styles = [
  ...SolidElement.styles,
  css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */@layer theme,base,components,utilities;
:host{display:block;height:100%}:host textarea{scrollbar-gutter:stable}:host([disabled]) ::-moz-placeholder,:host([visually-disabled]) ::-moz-placeholder{color:rgba(var(--sd-color-text-neutral-500))}:host([disabled]) ::placeholder,:host([visually-disabled]) ::placeholder{color:rgba(var(--sd-color-text-neutral-500))}:host([required]) #label::after{content:' *'}:host([visually-disabled]) textarea{caret-color:transparent}.no-scrollbar::-webkit-scrollbar{display:none}`
];
__decorateClass([
  query("#input")
], SdTextarea.prototype, "textarea", 2);
__decorateClass([
  query("#invalid-message")
], SdTextarea.prototype, "invalidMessage", 2);
__decorateClass([
  state()
], SdTextarea.prototype, "hasFocus", 2);
__decorateClass([
  state()
], SdTextarea.prototype, "showValidStyle", 2);
__decorateClass([
  state()
], SdTextarea.prototype, "showInvalidStyle", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "title", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "name", 2);
__decorateClass([
  property()
], SdTextarea.prototype, "value", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "size", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "label", 2);
__decorateClass([
  property({ type: String, attribute: "help-text", reflect: true })
], SdTextarea.prototype, "helpText", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "placeholder", 2);
__decorateClass([
  property({ type: Number, reflect: true })
], SdTextarea.prototype, "rows", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], SdTextarea.prototype, "disabled", 2);
__decorateClass([
  property({ type: Boolean, reflect: true, attribute: "visually-disabled" })
], SdTextarea.prototype, "visuallyDisabled", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], SdTextarea.prototype, "readonly", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "form", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], SdTextarea.prototype, "required", 2);
__decorateClass([
  property({ attribute: "floating-label", type: Boolean, reflect: true })
], SdTextarea.prototype, "floatingLabel", 2);
__decorateClass([
  property({ type: Number, reflect: true })
], SdTextarea.prototype, "minlength", 2);
__decorateClass([
  property({ type: Number, reflect: true })
], SdTextarea.prototype, "maxlength", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "autocapitalize", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "autocorrect", 2);
__decorateClass([
  property({ reflect: true })
], SdTextarea.prototype, "autocomplete", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], SdTextarea.prototype, "autofocus", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "enterkeyhint", 2);
__decorateClass([
  property({ type: Boolean, reflect: true, attribute: "style-on-valid" })
], SdTextarea.prototype, "styleOnValid", 2);
__decorateClass([
  property({
    type: Boolean,
    converter: {
      // Allow "true|false" attribute values but keep the property boolean
      fromAttribute: (value) => !value || value === "false" ? false : true,
      toAttribute: (value) => value ? "true" : "false"
    }
  })
], SdTextarea.prototype, "spellcheck", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTextarea.prototype, "inputmode", 2);
__decorateClass([
  defaultValue()
], SdTextarea.prototype, "defaultValue", 2);
__decorateClass([
  watch("disabled", { waitUntilFirstUpdate: true })
], SdTextarea.prototype, "handleDisabledChange", 1);
__decorateClass([
  watch("rows", { waitUntilFirstUpdate: true })
], SdTextarea.prototype, "handleRowsChange", 1);
__decorateClass([
  watch("value", { waitUntilFirstUpdate: true })
], SdTextarea.prototype, "handleValueChange", 1);
__decorateClass([
  watch(["size", "floatingLabel"])
], SdTextarea.prototype, "handleSizeChange", 1);
SdTextarea = __decorateClass([
  customElement("sd-6-22-0-textarea")
], SdTextarea);

export {
  SdTextarea
};
