var _a;
import { __awaiter, __decorate, __extends, __generator } from "tslib";
import Component from 'vue-class-component';
import { Prop } from 'vue-property-decorator';
import { InputState } from '../../mixins/input-state/input-state';
import FilePlugin, { DEFAULT_STORE_NAME } from '../../utils/file/file';
import uuid from '../../utils/uuid/uuid';
import { ModulVue } from '../../utils/vue/vue';
import { MButton, MButtonIconPosition, MButtonSkin } from '../button/button';
import { BUTTON_NAME, FILE_SELECT_NAME, I18N_NAME, VALIDATION_MESSAGE_NAME } from '../component-names';
import { MI18n } from '../i18n/i18n';
import { MValidationMessage } from '../validation-message/validation-message';
import WithRender from './file-select.html?style=./file-select.scss';
var MFileSelect = /** @class */ (function (_super) {
    __extends(MFileSelect, _super);
    function MFileSelect() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.id = "mFileSelect-" + uuid.generate();
        return _this;
    }
    MFileSelect.prototype.destroyed = function () {
        if (!this.keepStore) {
            this.$file.destroy(this.storeName);
        }
    };
    MFileSelect.prototype.onClick = function (event) {
        this.$refs.inputFile.click();
        this.$emit('click', event);
        this.$refs.inputFile.blur();
    };
    MFileSelect.prototype.onFocus = function (event) {
        this.$emit('focus');
    };
    MFileSelect.prototype.onBlur = function (event) {
        this.$emit('blur');
    };
    MFileSelect.prototype.processFile = function (event) {
        return __awaiter(this, void 0, void 0, function () {
            var file;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        file = this.$refs.inputFile.files;
                        if (!file) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.$file.add(file, this.storeName)];
                    case 1:
                        _a.sent();
                        this.$emit('file-selected');
                        _a.label = 2;
                    case 2:
                        this.$refs.inputFile.value = '';
                        return [2 /*return*/];
                }
            });
        });
    };
    Object.defineProperty(MFileSelect.prototype, "hasLabel", {
        get: function () {
            return !!this.label;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MFileSelect.prototype, "extensions", {
        get: function () {
            return this.allowedExtensions.map(function (ext) { return (ext.startsWith('.') ? '' : '.') + ext; }).join(', ');
        },
        enumerable: false,
        configurable: true
    });
    __decorate([
        Prop()
    ], MFileSelect.prototype, "label", void 0);
    __decorate([
        Prop({
            default: MButtonSkin.Secondary,
            validator: function (value) {
                return value === MButtonSkin.Primary || value === MButtonSkin.Secondary;
            }
        })
    ], MFileSelect.prototype, "skin", void 0);
    __decorate([
        Prop()
    ], MFileSelect.prototype, "disabled", void 0);
    __decorate([
        Prop()
    ], MFileSelect.prototype, "waiting", void 0);
    __decorate([
        Prop()
    ], MFileSelect.prototype, "fullSize", void 0);
    __decorate([
        Prop()
    ], MFileSelect.prototype, "iconName", void 0);
    __decorate([
        Prop({
            default: MButtonIconPosition.Left,
            validator: function (value) {
                return value === MButtonIconPosition.Left ||
                    value === MButtonIconPosition.Right;
            }
        })
    ], MFileSelect.prototype, "iconPosition", void 0);
    __decorate([
        Prop({ default: '12px' })
    ], MFileSelect.prototype, "iconSize", void 0);
    __decorate([
        Prop()
    ], MFileSelect.prototype, "multiple", void 0);
    __decorate([
        Prop({
            default: DEFAULT_STORE_NAME
        })
    ], MFileSelect.prototype, "storeName", void 0);
    __decorate([
        Prop({ default: false })
    ], MFileSelect.prototype, "keepStore", void 0);
    __decorate([
        Prop({ default: function () { return []; } })
    ], MFileSelect.prototype, "allowedExtensions", void 0);
    MFileSelect = __decorate([
        WithRender,
        Component({
            components: (_a = {},
                _a[BUTTON_NAME] = MButton,
                _a[I18N_NAME] = MI18n,
                _a[VALIDATION_MESSAGE_NAME] = MValidationMessage,
                _a),
            mixins: [InputState]
        })
    ], MFileSelect);
    return MFileSelect;
}(ModulVue));
export { MFileSelect };
var FileSelectPlugin = {
    install: function (v, options) {
        v.use(FilePlugin);
        v.component(FILE_SELECT_NAME, MFileSelect);
    }
};
export default FileSelectPlugin;
//# sourceMappingURL=file-select.js.map