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;
};
import { html } from 'lit';
import { property, state } from 'lit/decorators.js';
import { addThemingController } from '../../theming/theming-controller.js';
import IgcButtonComponent from '../button/button.js';
import { registerComponent } from '../common/definitions/register.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { FormValueFileListTransformers } from '../common/mixins/forms/form-transformers.js';
import { createFormValueState } from '../common/mixins/forms/form-value.js';
import { partMap } from '../common/part-map.js';
import { bindIf, isEmpty } from '../common/util.js';
import { IgcInputBaseComponent, } from '../input/input-base.js';
import IgcValidationContainerComponent from '../validation-container/validation-container.js';
import { styles } from './themes/file-input.base.css.js';
import { all } from './themes/themes.js';
import { fileValidators } from './validators.js';
class IgcFileInputComponent extends EventEmitterMixin(IgcInputBaseComponent) {
    static register() {
        registerComponent(IgcFileInputComponent, IgcValidationContainerComponent, IgcButtonComponent);
    }
    get __validators() {
        return fileValidators;
    }
    get _fileNames() {
        if (!this.files || this.files.length === 0)
            return null;
        return Array.from(this.files)
            .map((file) => file.name)
            .join(', ');
    }
    set value(value) {
        if (value === '' && this.input) {
            this.input.value = value;
        }
    }
    get value() {
        return this.input?.value ?? '';
    }
    get files() {
        return this.input?.files ?? null;
    }
    constructor() {
        super();
        this._formValue = createFormValueState(this, {
            initialValue: null,
            transformers: FormValueFileListTransformers,
        });
        this._hasActivation = false;
        this.multiple = false;
        this.accept = '';
        this.readOnly = false;
        addThemingController(this, all);
    }
    _restoreDefaultValue() {
        this.input.value = '';
        super._restoreDefaultValue();
    }
    setSelectionRange() { }
    setRangeText() { }
    _handleChange() {
        this._hasActivation = false;
        this._setTouchedState();
        this._formValue.setValueAndFormState(this.files);
        this.requestUpdate();
        this.emitEvent('igcChange', { detail: this.files });
    }
    _handleCancel() {
        this._hasActivation = false;
        this._setTouchedState();
        this._validate();
        this.emitEvent('igcCancel', {
            detail: this.files,
        });
    }
    _handleBlur() {
        this._hasActivation ? this._validate() : super._handleBlur();
    }
    _handleClick() {
        this._hasActivation = true;
    }
    renderFileParts() {
        const emptyText = this.placeholder ?? 'No file chosen';
        return html `
      <div part="file-parts">
        <div part="file-selector-button">
          <igc-button variant="flat" ?disabled=${this.disabled} tabindex="-1">
            <slot name="file-selector-text">Browse</slot>
          </igc-button>
        </div>
        <div part="file-names">
          <span>
            ${this._fileNames ??
            html `<slot name="file-missing-text">${emptyText}</slot>`}
          </span>
        </div>
      </div>
    `;
    }
    renderInput() {
        const hasNegativeTabIndex = this.getAttribute('tabindex') === '-1';
        const hasHelperText = !isEmpty(this._helperText);
        return html `
      <input
        id=${this.inputId}
        part=${partMap(this.resolvePartNames('input'))}
        type="file"
        ?disabled=${this.disabled}
        ?required=${this.required}
        ?autofocus=${this.autofocus}
        ?multiple=${this.multiple}
        tabindex=${bindIf(hasNegativeTabIndex, -1)}
        accept=${bindIf(this.accept, this.accept)}
        aria-describedby=${bindIf(hasHelperText, 'helper-text')}
        @click=${this._handleClick}
        @change=${this._handleChange}
        @cancel=${this._handleCancel}
        @blur=${this._handleBlur}
      />
    `;
    }
}
IgcFileInputComponent.tagName = 'igc-file-input';
IgcFileInputComponent.styles = [...IgcInputBaseComponent.styles, styles];
export default IgcFileInputComponent;
__decorate([
    state()
], IgcFileInputComponent.prototype, "_hasActivation", void 0);
__decorate([
    property()
], IgcFileInputComponent.prototype, "value", null);
__decorate([
    property({ type: Boolean })
], IgcFileInputComponent.prototype, "multiple", void 0);
__decorate([
    property({ type: String })
], IgcFileInputComponent.prototype, "accept", void 0);
__decorate([
    property({ type: Boolean })
], IgcFileInputComponent.prototype, "autofocus", void 0);
__decorate([
    property({ type: Boolean, attribute: false, noAccessor: true })
], IgcFileInputComponent.prototype, "readOnly", void 0);
//# sourceMappingURL=file-input.js.map