// #region Imports
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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { on, HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { Disableable } from '../../../Behaviors/Disableable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { filePickerElementTemplate } from './FilePickerElementTemplate';
import { filePickerElementJoyStyle } from './Themes/FilePickerElement.Joy';
import { filePickerElementRetroStyle } from './Themes/FilePickerElement.Retro';
import { filePickerElementCosmopolitanStyle } from './Themes/FilePickerElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
// #endregion
/**
 * The `{@link FilePickerElement}` element.
 *
 * @public
 */
let FilePickerElement = class FilePickerElement extends Themeable(Disableable(CustomElement)) {
    // #region Fields
    _filePicked;
    _fileElement;
    _multiple;
    _accept;
    _directory;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._multiple = false;
        this._accept = [];
        this._directory = false;
        this._filePicked = new HTMLElementEventEmitter(this, 'filePicked');
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     * The `{@link is}` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-file-picker';
    }
    /**
     * Gets or sets the `multiple` property.
     *
     * @public
     * @attr
     */
    get multiple() {
        return this._multiple;
    }
    set multiple(value) {
        if (this._multiple !== value) {
            this._multiple = value;
            this.requestUpdate('multiple');
        }
    }
    /**
     * Gets or sets the `accept` property.
     *
     * @public
     */
    get accept() {
        return this._accept;
    }
    set accept(value) {
        if (this._accept !== value) {
            this._accept = value;
            this.requestUpdate('accept');
        }
    }
    /**
     * Gets or sets the `directory` property.
     *
     * @public
     * @attr
     */
    get directory() {
        return this._directory;
    }
    set directory(value) {
        if (this._directory !== value) {
            this._directory = value;
            this.requestUpdate('directory');
        }
    }
    /**
     * Called when a file is picked.
     * Provides reference to `{@link IEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get filePicked() {
        return this._filePicked;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    pick() {
        if (this.disabled) {
            return;
        }
        this._fileElement.click();
    }
    /**
     * @private
     * @template
     */
    onFileChange(e) {
        const files = e.target.files;
        if (files) {
            this.onFilePicked({ files: files });
            // reset input value, so the same file can be uploaded multiple times.
            e.target.value = '';
        }
    }
    /**
     * @private
     * @template
     */
    onSlotChange(e) {
        const slot = e.target;
        const assignedElements = slot.assignedElements();
        if (assignedElements.length > 0) {
            const firstElement = assignedElements[0];
            on(firstElement, 'click', () => this.pick());
        }
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        this._fileElement = this.getTemplatePart('file');
    }
    /**
     * Emits the {@link filePicked} event.
     *
     * @protected
     */
    onFilePicked(args) {
        this._filePicked.emit(args);
    }
};
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], FilePickerElement.prototype, "multiple", null);
__decorate([
    Property({ type: (Array) }),
    __metadata("design:type", Array),
    __metadata("design:paramtypes", [Array])
], FilePickerElement.prototype, "accept", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], FilePickerElement.prototype, "directory", null);
__decorate([
    Emit({ eventName: 'filePicked' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], FilePickerElement.prototype, "filePicked", null);
FilePickerElement = __decorate([
    Component({
        selector: 'mosaik-file-picker',
        template: filePickerElementTemplate,
        themes: {
            joy: filePickerElementJoyStyle,
            retro: filePickerElementRetroStyle,
            cosmopolitan: filePickerElementCosmopolitanStyle
        },
        imports: []
    }),
    __metadata("design:paramtypes", [])
], FilePickerElement);
export { FilePickerElement };
//# sourceMappingURL=FilePickerElement.js.map