import { nothing, html } from 'lit';
import { property, state } from 'lit/decorators.js';
import { tv } from 'tailwind-variants';
import { a as TailwindStyledElement, s as safeCustomElement } from '../../chunks/index-DluTRl1F.js';
import '../../tailwind-config.js';
import '../../chunks/index-CSnDkMHN.js';
import { c as cn } from '../../chunks/cn-Df_5FiGi.js';
import 'viem';
import 'axe-core';
import '../lukso-icon/index.js';
import '../../chunks/index-CA_aKopU.js';
import '../lukso-form-label/index.js';
import '../lukso-form-description/index.js';
import '../lukso-form-error/index.js';

const style = ":host {\n\n    display: inline-flex\n}\n\n:host([is-full-width]) {\n\n    display: flex;\n\n    width: 100%\n}";

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;
};
const FOCUS_DELAY_MS = 10;
let LuksoTextarea = class extends TailwindStyledElement(style) {
  constructor() {
    super(...arguments);
    this.value = "";
    this.name = "";
    this.placeholder = "";
    this.label = "";
    this.id = "";
    this.ref = "";
    this.description = "";
    this.error = "";
    this.customClass = "";
    this.isFullWidth = false;
    this.isReadonly = false;
    this.isDisabled = false;
    this.autofocus = false;
    this.borderless = false;
    this.isNonResizable = false;
    this.keepFocusOnEscape = false;
    this.size = "large";
    this.rows = 4;
    this.hasFocus = false;
    this.hasHighlight = false;
    this.styles = tv({
      slots: {
        wrapper: "group flex",
        input: `bg-neutral-100 border-solid placeholder:text-neutral-70 w-full
        outline-none transition transition-all duration-150 appearance-none`
      },
      variants: {
        hasError: {
          true: {
            input: "border-red-85"
          },
          false: {
            input: "border-neutral-90"
          }
        },
        hasHighlight: {
          true: {
            input: "border-neutral-35"
          }
        },
        borderless: {
          true: { input: "border-0" },
          false: { input: "border" }
        },
        isReadonly: {
          true: { input: "cursor-not-allowed" }
        },
        isDisabled: {
          true: {
            input: "text-neutral-60 cursor-not-allowed"
          },
          false: { input: "text-neutral-20" }
        },
        isFullWidth: {
          true: { wrapper: "w-full" },
          false: { wrapper: "w-[350px]" }
        },
        size: {
          small: {
            input: "min-h-7 px-2 py-1 paragraph-inter-12-regular rounded-8"
          },
          medium: {
            input: "min-h-10 px-3 py-3 paragraph-inter-14-regular rounded-10"
          },
          large: {
            input: "min-h-12 px-4 py-3 paragraph-inter-14-regular rounded-12"
          },
          "x-large": {}
        },
        isNonResizable: {
          true: {
            input: "resize-none"
          }
        }
      },
      compoundVariants: [
        {
          isFullWidth: false,
          size: "small",
          class: { wrapper: "w-[210px]" }
        },
        {
          hasHighlight: true,
          hasError: true,
          class: {
            input: "border-red-65"
          }
        }
      ]
    });
  }
  connectedCallback() {
    super.connectedCallback();
    if (this.autofocus) {
      setTimeout(() => {
        const textarea = this.shadowRoot?.querySelector("textarea");
        textarea?.focus();
      }, FOCUS_DELAY_MS);
    }
  }
  inputTemplate(styles) {
    return html`
      <textarea
        name=${this.name ? this.name : nothing}
        placeholder=${this.placeholder ? this.placeholder : nothing}
        ref=${this.ref ? this.ref : nothing}
        id=${this.id ? this.id : nothing}
        rows=${this.rows}
        data-testid=${this.name ? `textarea-${this.name}` : "textarea"}
        ?autofocus=${this.autofocus}
        ?readonly=${this.isReadonly ? true : void 0}
        ?disabled=${this.isDisabled ? true : void 0}
        class=${cn(styles, this.customClass)}
        @focus=${this.handleFocus}
        @input=${this.handleInput}
        @change=${this.handleChange}
        @blur=${this.handleBlur}
        @keyup=${this.handleKeyUp}
        @keydown=${this.handleKeyDown}
        @keypress=${this.handleKeyPress}
        @mouseenter=${this.handleMouseOver}
        @mouseleave=${this.handleMouseOut}
        @click=${this.handleInputClick}
      >
${this.value}</textarea
      >
    `;
  }
  handleFocus() {
    if (!this.isReadonly && !this.isDisabled) {
      this.hasFocus = true;
      this.hasHighlight = true;
    }
  }
  async handleBlur(event) {
    this.hasFocus = false;
    this.hasHighlight = false;
    await this.updateComplete;
    const target = event.target;
    const blurEvent = new CustomEvent("on-blur", {
      detail: {
        value: target?.value,
        event
      },
      bubbles: false,
      composed: true
    });
    this.dispatchEvent(blurEvent);
  }
  async handleChange(event) {
    await this.updateComplete;
    const target = event.target;
    const changeEvent = new CustomEvent("on-change", {
      detail: {
        value: target?.value,
        event
      },
      bubbles: false,
      composed: true
    });
    this.dispatchEvent(changeEvent);
  }
  async handleInput(event) {
    const target = event.target;
    this.value = target?.value;
    await this.updateComplete;
    const changeEvent = new CustomEvent("on-input", {
      detail: {
        value: target?.value,
        event
      },
      bubbles: false,
      composed: true
    });
    this.dispatchEvent(changeEvent);
  }
  async handleKeyUp(event) {
    await this.updateComplete;
    const target = event.target;
    const keyEvent = new CustomEvent("on-key-up", {
      detail: {
        value: target?.value,
        event
      },
      bubbles: true,
      composed: true
    });
    this.dispatchEvent(keyEvent);
    if (event.key === "Escape" && !this.keepFocusOnEscape) {
      this.blur();
    }
  }
  async handleKeyDown(event) {
    await this.updateComplete;
    const target = event.target;
    const keyEvent = new CustomEvent("on-key-down", {
      detail: {
        value: target.value,
        event
      },
      bubbles: true,
      composed: true
    });
    this.dispatchEvent(keyEvent);
  }
  async handleKeyPress(event) {
    await this.updateComplete;
    const target = event.target;
    const keyEvent = new CustomEvent("on-key-press", {
      detail: {
        value: target?.value,
        event
      },
      bubbles: true,
      composed: true
    });
    this.dispatchEvent(keyEvent);
  }
  handleMouseOver() {
    if (!this.isReadonly && !this.isDisabled) {
      this.hasHighlight = true;
    }
  }
  handleMouseOut() {
    if (!this.hasFocus) {
      this.hasHighlight = false;
    }
  }
  async handleInputClick(event) {
    await this.updateComplete;
    const target = event.target;
    const clickEvent = new CustomEvent("on-input-click", {
      detail: {
        value: target.value,
        event
      },
      bubbles: false,
      composed: true
    });
    this.dispatchEvent(clickEvent);
  }
  render() {
    const { wrapper, input } = this.styles({
      hasError: !!this.error,
      hasHighlight: this.hasHighlight,
      borderless: this.borderless,
      isReadonly: this.isReadonly,
      isDisabled: this.isDisabled,
      isFullWidth: this.isFullWidth,
      size: this.size,
      isNonResizable: this.isNonResizable
    });
    return html`
      <div class="w-[inherit]">
        <lukso-form-label
          for-name=${this.name}
          label=${this.label}
        ></lukso-form-label>
        <lukso-form-description
          description=${this.description}
        ></lukso-form-description>
        <div class=${wrapper()}>
          <div class="relative w-[inherit] flex">
            ${this.inputTemplate(input())}
          </div>
        </div>
        <lukso-form-error error=${this.error}></lukso-form-error>
      </div>
    `;
  }
};
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "value", 2);
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "name", 2);
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "placeholder", 2);
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "label", 2);
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "id", 2);
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "ref", 2);
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "description", 2);
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "error", 2);
__decorateClass([
  property({ type: String, attribute: "custom-class" })
], LuksoTextarea.prototype, "customClass", 2);
__decorateClass([
  property({ type: Boolean, attribute: "is-full-width" })
], LuksoTextarea.prototype, "isFullWidth", 2);
__decorateClass([
  property({ type: Boolean, attribute: "is-readonly" })
], LuksoTextarea.prototype, "isReadonly", 2);
__decorateClass([
  property({ type: Boolean, attribute: "is-disabled" })
], LuksoTextarea.prototype, "isDisabled", 2);
__decorateClass([
  property({ type: Boolean })
], LuksoTextarea.prototype, "autofocus", 2);
__decorateClass([
  property({ type: Boolean })
], LuksoTextarea.prototype, "borderless", 2);
__decorateClass([
  property({ type: Boolean, attribute: "is-non-resizable" })
], LuksoTextarea.prototype, "isNonResizable", 2);
__decorateClass([
  property({ type: Boolean, attribute: "keep-focus-on-escape" })
], LuksoTextarea.prototype, "keepFocusOnEscape", 2);
__decorateClass([
  property({ type: String })
], LuksoTextarea.prototype, "size", 2);
__decorateClass([
  property({ type: Number })
], LuksoTextarea.prototype, "rows", 2);
__decorateClass([
  state()
], LuksoTextarea.prototype, "hasFocus", 2);
__decorateClass([
  state()
], LuksoTextarea.prototype, "hasHighlight", 2);
LuksoTextarea = __decorateClass([
  safeCustomElement("lukso-textarea")
], LuksoTextarea);

export { LuksoTextarea };
