var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var IgcTextareaComponent_1;
import { LitElement, html, nothing } from 'lit';
import { property, query, queryAssignedElements, queryAssignedNodes, } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
import { styleMap } from 'lit/directives/style-map.js';
import { getThemeController, themes } from '../../theming/theming-decorator.js';
import { watch } from '../common/decorators/watch.js';
import { registerComponent } from '../common/definitions/register.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { FormAssociatedRequiredMixin } from '../common/mixins/forms/associated-required.js';
import { createFormValueState, } from '../common/mixins/forms/form-value.js';
import { asNumber, createCounter, isEmpty, partNameMap, } from '../common/util.js';
import IgcValidationContainerComponent from '../validation-container/validation-container.js';
import { styles as shared } from './themes/shared/textarea.common.css.js';
import { styles } from './themes/textarea.base.css.js';
import { all } from './themes/themes.js';
import { textAreaValidators } from './validators.js';
let IgcTextareaComponent = IgcTextareaComponent_1 = class IgcTextareaComponent extends FormAssociatedRequiredMixin(EventEmitterMixin(LitElement)) {
    static register() {
        registerComponent(IgcTextareaComponent_1, IgcValidationContainerComponent);
    }
    get __validators() {
        return textAreaValidators;
    }
    get resizeStyles() {
        return {
            resize: this.resize === 'auto' ? 'none' : this.resize,
        };
    }
    get _isMaterial() {
        return getThemeController(this)?.theme === 'material';
    }
    set value(value) {
        this._formValue.setValueAndFormState(value);
        this._validate();
    }
    get value() {
        return this._formValue.value;
    }
    async valueChanged() {
        await this.updateComplete;
        this.setAreaHeight();
    }
    setAreaHeight() {
        if (this.resize === 'auto') {
            this.input.style.height = 'auto';
            this.input.style.height = `${this.setAutoHeight()}px`;
        }
        else {
            Object.assign(this.input.style, { height: undefined });
        }
    }
    constructor() {
        super();
        this.inputId = `textarea-${IgcTextareaComponent_1.increment()}`;
        this.outlined = false;
        this.readOnly = false;
        this.resize = 'vertical';
        this.rows = 2;
        this.spellcheck = true;
        this.wrap = 'soft';
        this.validateOnly = false;
        this._formValue = createFormValueState(this, { initialValue: '' });
        this.addEventListener('focus', this.handleFocus);
        this.addEventListener('blur', this.handleBlur);
    }
    async connectedCallback() {
        super.connectedCallback();
        await this.updateComplete;
        this.setAreaHeight();
        this.observer = new ResizeObserver(() => this.setAreaHeight());
        this.observer.observe(this.input);
    }
    disconnectedCallback() {
        this.observer.disconnect();
        super.disconnectedCallback();
    }
    select() {
        this.input.select();
    }
    setSelectionRange(start, end, direction = 'none') {
        this.input.setSelectionRange(start, end, direction);
    }
    setRangeText(replacement, start, end, selectMode = 'preserve') {
        this.input.setRangeText(replacement, start, end, selectMode);
        this.value = this.input.value;
    }
    scrollTo(x, y) {
        x !== undefined && y !== undefined
            ? this.input.scrollTo(x, y)
            : this.input.scrollTo(x);
    }
    resolvePartNames() {
        return {
            container: true,
            prefixed: this.prefixes.length > 0,
            suffixed: this.suffixes.length > 0,
            filled: !!this.value,
        };
    }
    setAutoHeight() {
        const { borderTopWidth, borderBottomWidth } = getComputedStyle(this.input);
        return (this.input.scrollHeight +
            asNumber(borderTopWidth) +
            asNumber(borderBottomWidth));
    }
    handleInput() {
        this.value = this.input.value;
        this.emitEvent('igcInput', { detail: this.value });
    }
    handleChange() {
        this.value = this.input.value;
        this.emitEvent('igcChange', { detail: this.value });
    }
    handleFocus() {
        this._dirty = true;
    }
    handleBlur() {
        this._validate();
    }
    valueSlotChange() {
        const value = this.projected
            .map((node) => node.textContent?.trim())
            .filter((node) => Boolean(node));
        if (value.length) {
            this.value = value.join('\r\n');
        }
    }
    renderValueSlot() {
        return html `
      <slot style="display: none" @slotchange=${this.valueSlotChange}></slot>
    `;
    }
    renderValidationContainer() {
        return IgcValidationContainerComponent.create(this);
    }
    renderPrefix() {
        return html `
      <div part="prefix" .hidden=${isEmpty(this.prefixes)}>
        <slot name="prefix"></slot>
      </div>
    `;
    }
    renderSuffix() {
        return html `
      <div part="suffix" .hidden=${isEmpty(this.suffixes)}>
        <slot name="suffix"></slot>
      </div>
    `;
    }
    renderLabel() {
        return this.label
            ? html `
          <label part="label" for=${this.id || this.inputId}>
            ${this.label}
          </label>
        `
            : nothing;
    }
    renderStandard() {
        return html `
      ${this.renderLabel()}
      <div part=${partNameMap(this.resolvePartNames())}>
        ${this.renderPrefix()} ${this.renderInput()} ${this.renderSuffix()}
      </div>
      ${this.renderValidationContainer()}
    `;
    }
    renderMaterial() {
        return html `
      <div
        part=${partNameMap({
            ...this.resolvePartNames(),
            labelled: this.label,
            placeholder: this.placeholder,
        })}
      >
        <div part="start">${this.renderPrefix()}</div>
        ${this.renderInput()}
        <div part="notch">${this.renderLabel()}</div>
        <div part="filler"></div>
        <div part="end">${this.renderSuffix()}</div>
      </div>
      ${this.renderValidationContainer()}
    `;
    }
    renderInput() {
        return html `
      ${this.renderValueSlot()}
      <textarea
        id=${this.id || this.inputId}
        part="input"
        style=${styleMap(this.resizeStyles)}
        @input=${this.handleInput}
        @change=${this.handleChange}
        placeholder=${ifDefined(this.placeholder)}
        .rows=${this.rows}
        .value=${live(this.value)}
        .wrap=${this.wrap}
        autocomplete=${ifDefined(this.autocomplete)}
        autocapitalize=${ifDefined(this.autocapitalize)}
        inputmode=${ifDefined(this.inputMode)}
        spellcheck=${ifDefined(this.spellcheck)}
        minlength=${ifDefined(this.minLength)}
        maxlength=${ifDefined(this.validateOnly ? undefined : this.maxLength)}
        ?disabled=${this.disabled}
        ?required=${this.required}
        ?readonly=${this.readOnly}
        aria-invalid=${this.invalid ? 'true' : 'false'}
      ></textarea>
    `;
    }
    render() {
        return this._isMaterial ? this.renderMaterial() : this.renderStandard();
    }
};
IgcTextareaComponent.tagName = 'igc-textarea';
IgcTextareaComponent.styles = [styles, shared];
IgcTextareaComponent.shadowRootOptions = {
    ...LitElement.shadowRootOptions,
    delegatesFocus: true,
};
IgcTextareaComponent.increment = createCounter();
__decorate([
    queryAssignedNodes({ flatten: true })
], IgcTextareaComponent.prototype, "projected", void 0);
__decorate([
    queryAssignedElements({
        slot: 'prefix',
        selector: '[slot="prefix"]:not([hidden])',
    })
], IgcTextareaComponent.prototype, "prefixes", void 0);
__decorate([
    queryAssignedElements({
        slot: 'suffix',
        selector: '[slot="suffix"]:not([hidden])',
    })
], IgcTextareaComponent.prototype, "suffixes", void 0);
__decorate([
    query('textarea', true)
], IgcTextareaComponent.prototype, "input", void 0);
__decorate([
    property()
], IgcTextareaComponent.prototype, "autocomplete", void 0);
__decorate([
    property()
], IgcTextareaComponent.prototype, "autocapitalize", void 0);
__decorate([
    property({ attribute: 'inputmode' })
], IgcTextareaComponent.prototype, "inputMode", void 0);
__decorate([
    property()
], IgcTextareaComponent.prototype, "label", void 0);
__decorate([
    property({ type: Number, attribute: 'maxlength' })
], IgcTextareaComponent.prototype, "maxLength", void 0);
__decorate([
    property({ type: Number, attribute: 'minlength' })
], IgcTextareaComponent.prototype, "minLength", void 0);
__decorate([
    property({ reflect: true, type: Boolean })
], IgcTextareaComponent.prototype, "outlined", void 0);
__decorate([
    property()
], IgcTextareaComponent.prototype, "placeholder", void 0);
__decorate([
    property({ type: Boolean, reflect: true, attribute: 'readonly' })
], IgcTextareaComponent.prototype, "readOnly", void 0);
__decorate([
    property()
], IgcTextareaComponent.prototype, "resize", void 0);
__decorate([
    property({ type: Number })
], IgcTextareaComponent.prototype, "rows", void 0);
__decorate([
    property()
], IgcTextareaComponent.prototype, "value", null);
__decorate([
    property({
        type: Boolean,
        converter: {
            fromAttribute: (value) => !(!value || value === 'false'),
            toAttribute: (value) => (value ? 'true' : 'false'),
        },
    })
], IgcTextareaComponent.prototype, "spellcheck", void 0);
__decorate([
    property()
], IgcTextareaComponent.prototype, "wrap", void 0);
__decorate([
    property({ type: Boolean, reflect: true, attribute: 'validate-only' })
], IgcTextareaComponent.prototype, "validateOnly", void 0);
__decorate([
    watch('value')
], IgcTextareaComponent.prototype, "valueChanged", null);
__decorate([
    watch('rows', { waitUntilFirstUpdate: true }),
    watch('resize', { waitUntilFirstUpdate: true })
], IgcTextareaComponent.prototype, "setAreaHeight", null);
IgcTextareaComponent = IgcTextareaComponent_1 = __decorate([
    themes(all, { exposeController: true })
], IgcTextareaComponent);
export default IgcTextareaComponent;
//# sourceMappingURL=textarea.js.map