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 IgcFileInputComponent_1;
import { html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { themes } from '../../theming/theming-decorator.js';
import IgcButtonComponent from '../button/button.js';
import { registerComponent } from '../common/definitions/register.js';
import { createFormValueState, defaultFileListTransformer, } from '../common/mixins/forms/form-value.js';
import { isEmpty, partNameMap } 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';
let IgcFileInputComponent = IgcFileInputComponent_1 = class IgcFileInputComponent extends IgcInputBaseComponent {
    static register() {
        registerComponent(IgcFileInputComponent_1, 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._hasActivation = false;
        this.multiple = false;
        this.accept = '';
        this.tabIndex = 0;
        this.readOnly = false;
        this._formValue = createFormValueState(this, {
            initialValue: null,
            transformers: defaultFileListTransformer,
        });
    }
    _restoreDefaultValue() {
        this.input.value = '';
        super._restoreDefaultValue();
    }
    setSelectionRange() { }
    setRangeText() { }
    _handleChange() {
        this._hasActivation = false;
        this._formValue.setValueAndFormState(this.files);
        this._validate();
        this.requestUpdate();
        this.emitEvent('igcChange', { detail: this.value });
    }
    _handleCancel() {
        this._hasActivation = false;
        this._validate();
        this.emitEvent('igcCancel', {
            detail: this.value,
        });
    }
    _handleFocus() {
        this._dirty = true;
    }
    _handleBlur() {
        if (!this._hasActivation) {
            this._validate();
        }
    }
    _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">
          ${this._fileNames ??
            html `<slot name="file-missing-text">${emptyText}</slot>`}
        </div>
      </div>
    `;
    }
    renderInput() {
        return html `
      <input
        id=${this.inputId}
        part=${partNameMap(this.resolvePartNames('input'))}
        type="file"
        ?disabled=${this.disabled}
        ?required=${this.required}
        ?autofocus=${this.autofocus}
        ?multiple=${this.multiple}
        tabindex=${this.tabIndex}
        accept=${ifDefined(this.accept === '' ? undefined : this.accept)}
        aria-invalid=${this.invalid}
        aria-describedby=${ifDefined(isEmpty(this._helperText) ? nothing : 'helper-text')}
        @click=${this._handleClick}
        @change=${this._handleChange}
        @cancel=${this._handleCancel}
        @focus=${this._handleFocus}
        @blur=${this._handleBlur}
      />
    `;
    }
};
IgcFileInputComponent.tagName = 'igc-file-input';
IgcFileInputComponent.styles = [...IgcInputBaseComponent.styles, styles];
__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: Number })
], IgcFileInputComponent.prototype, "tabIndex", void 0);
__decorate([
    property({ type: Boolean, attribute: false, noAccessor: true })
], IgcFileInputComponent.prototype, "readOnly", void 0);
IgcFileInputComponent = IgcFileInputComponent_1 = __decorate([
    themes(all)
], IgcFileInputComponent);
export default IgcFileInputComponent;
//# sourceMappingURL=file-input.js.map