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);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import { UIPictures } from '../pictures/ui-pictures';
import { bindable, autoinject, customElement, bindingMode, BindingEngine, processContent, children } from 'aurelia-framework';
import { EnumerationTypeService } from '../../services/enumeration-type-service';
import { observable } from 'aurelia-binding';
import { PictureHelper } from './picture-helper';
import { Config } from '../../config';
import { RemoteFileStorage } from '../../services/file-storage-service';
export var UIFiles = (function () {
    function UIFiles(bindingEngine, pictureHelper, config, fileStorageService) {
        this.bindingEngine = bindingEngine;
        this.pictureHelper = pictureHelper;
        this.config = config;
        this.fileStorageService = fileStorageService;
        this.enumerationDisplayPropertyName = 'fileType';
        this.enumerationIdPropertyName = 'fileTypeId';
        this.accept = 'all';
        this.canAdd = true;
        this.canEdit = true;
        this.canDownload = true;
        this.canDelete = false;
        this.isSelectable = false;
        this.acceptSelectable = 'all';
        this.autoConvert = false;
        this.dropZoneMode = 'Inner';
        this.designWithBorder = false;
        this.uploadBtnPosition = "Auto";
        this.ignoreFileExistence = false;
        this.useVirtualization = false;
        this.customColumns = [];
        this.filesSearched = [];
        this.disposables = [];
        this.fileTypeService = new EnumerationTypeService(this.enumerationCategory);
    }
    UIFiles.prototype.showColumn = function (columnName) {
        if (this.columnsInfo == null || this.columnsInfo.length == 0)
            return true;
        var i = this.columnsInfo.findIndex(function (x) { return x.columnName == columnName; });
        if (i > -1) {
            return this.columnsInfo[i].columnVisibility;
        }
        return true;
    };
    UIFiles.prototype.getWidthColumn = function (columnName) {
        var _a;
        if (this.columnsInfo == null || this.columnsInfo.length == 0)
            return true;
        var i = this.columnsInfo.findIndex(function (x) { return x.columnName == columnName; });
        if (i > -1) {
            return (_a = this.columnsInfo[i].width) !== null && _a !== void 0 ? _a : 1;
        }
        return 1;
    };
    UIFiles.prototype.enumerationCategoryChanged = function (newVal, oldVal) {
        this.fileTypeService.gridDataSource.queryParameters = { category: this.enumerationCategory, onlyActifs: true };
    };
    UIFiles.prototype.bind = function () {
        var _this = this;
        var _a;
        this.fileTypeService.gridDataSource.queryParameters = { category: this.enumerationCategory, onlyActifs: true };
        if (this.columnsInfo == null || ((_a = this.columnsInfo) === null || _a === void 0 ? void 0 : _a.length) == 0) {
            this.columnsInfo = this.config.globalConfig.defaultUIFilesColumnsInfo;
        }
        else {
            this.config.globalConfig.defaultUIFilesColumnsInfo.forEach(function (defaultName) {
                var i = _this.columnsInfo.findIndex(function (x) { return x.columnName == defaultName.columnName; });
                if (i == -1) {
                    _this.columnsInfo.push(defaultName);
                }
            });
        }
        if (this.selectedFiles == null) {
            this.selectedFiles = [];
        }
        if (this.canDownload && !this.fileStorageService.canDownloadFiles) {
            this.canDownload = false;
        }
    };
    UIFiles.prototype.attached = function () {
        var _this = this;
        this.disposables.forEach(function (d) { return d.dispose(); });
        this.disposables.splice(0);
        this.files.forEach(function (file) {
            if (_this.isSelectable) {
                _this.addObserverIsSelected(file);
            }
            file['enumerationDisplay'] = file[_this.enumerationDisplayPropertyName];
            file['enumerationId'] = file[_this.enumerationIdPropertyName];
        });
        this.disposables.push(this.bindingEngine.collectionObserver(this.files).subscribe(function (changeRecords) {
            if (changeRecords.length > 0 && changeRecords[0].addedCount > 0) {
                var index = changeRecords[0].index;
                while (index < _this.files.length) {
                    var file = _this.files[index];
                    if (_this.isSelectable) {
                        _this.addObserverIsSelected(file);
                    }
                    file['enumerationDisplay'] = file[_this.enumerationDisplayPropertyName];
                    file['enumerationId'] = file[_this.enumerationIdPropertyName];
                    index++;
                }
            }
        }));
        this.disposables.push(this.bindingEngine.collectionObserver(this.files).subscribe(function (changeRecords) {
            _this.resetFilteredFiles();
        }));
        this.resetFilteredFiles();
    };
    UIFiles.prototype.addObserverIsSelected = function (file) {
        var _this = this;
        if (file.extension == null) {
            file.extension = this.pictureHelper.getFileExtension(file.url);
        }
        file.isSelectable = (this.acceptSelectable == 'all' || this.acceptSelectable.includes(file.extension));
        var observer = this.bindingEngine.propertyObserver(file, 'isSelected');
        var disposable = observer.subscribe(function (newVal, oldVal) {
            if (newVal === true) {
                _this.selectedFiles.push(file);
            }
            else {
                _this.selectedFiles.remove(file);
            }
        });
        this.disposables.push(disposable);
    };
    UIFiles.prototype.detached = function () {
        this.disposables.forEach(function (d) { return d.dispose(); });
    };
    UIFiles.prototype.resetFilteredFiles = function () {
        var _a, _b;
        var _this = this;
        var _c;
        if (this.filesSearched != null) {
            this.filesSearched.splice(0);
        }
        else {
            this.filesSearched = [];
        }
        if (this.currentSearch != null && ((_c = this.currentSearch.trim()) === null || _c === void 0 ? void 0 : _c.length) > 0) {
            (_a = this.filesSearched).push.apply(_a, __spreadArray([], __read(this.files.filter(function (file) { var _a, _b; return ((_a = file.name) === null || _a === void 0 ? void 0 : _a.toUpperCase().includes(_this.currentSearch.toUpperCase())) || ((_b = file.description) === null || _b === void 0 ? void 0 : _b.toUpperCase().includes(_this.currentSearch.toUpperCase())); })), false));
        }
        else {
            (_b = this.filesSearched).push.apply(_b, __spreadArray([], __read(this.files), false));
        }
    };
    UIFiles.prototype.currentSearchChanged = function (n, o) {
        if (n != o) {
            this.resetFilteredFiles();
        }
    };
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", Array)
    ], UIFiles.prototype, "files", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", Number)
    ], UIFiles.prototype, "enumerationCategory", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", String)
    ], UIFiles.prototype, "enumerationDisplayPropertyName", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", String)
    ], UIFiles.prototype, "enumerationIdPropertyName", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Function)
    ], UIFiles.prototype, "fileAddedFn", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Function)
    ], UIFiles.prototype, "fileUploadedFn", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Function)
    ], UIFiles.prototype, "confirmUploadFn", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Function)
    ], UIFiles.prototype, "uploadCompleteFn", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", String)
    ], UIFiles.prototype, "accept", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", String)
    ], UIFiles.prototype, "folderName", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", Boolean)
    ], UIFiles.prototype, "canAdd", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", Boolean)
    ], UIFiles.prototype, "canEdit", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", Boolean)
    ], UIFiles.prototype, "canDownload", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", Boolean)
    ], UIFiles.prototype, "canDelete", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Function)
    ], UIFiles.prototype, "deleteFileFn", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Function)
    ], UIFiles.prototype, "deleteFilesFn", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Function)
    ], UIFiles.prototype, "massiveDownloadFn", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", Array)
    ], UIFiles.prototype, "selectedFiles", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", Boolean)
    ], UIFiles.prototype, "isSelectable", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", String)
    ], UIFiles.prototype, "acceptSelectable", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Array)
    ], UIFiles.prototype, "columnsInfo", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Object)
    ], UIFiles.prototype, "autoConvert", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.twoWay }),
        __metadata("design:type", String)
    ], UIFiles.prototype, "dropZoneMode", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Boolean)
    ], UIFiles.prototype, "designWithBorder", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", HTMLElement)
    ], UIFiles.prototype, "uploadBtn", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", String)
    ], UIFiles.prototype, "uploadBtnPosition", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Boolean)
    ], UIFiles.prototype, "ignoreFileExistence", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.oneTime }),
        __metadata("design:type", Boolean)
    ], UIFiles.prototype, "useVirtualization", void 0);
    __decorate([
        observable,
        __metadata("design:type", String)
    ], UIFiles.prototype, "currentSearch", void 0);
    UIFiles = __decorate([
        autoinject,
        customElement('ui-files'),
        children({ name: 'customColumns', selector: ['ui-column'] }),
        processContent(function (compiler, resources, node, instruction) {
            instruction.inheritBindingContext = true;
            return true;
        }),
        __metadata("design:paramtypes", [BindingEngine, PictureHelper, Config, RemoteFileStorage.Service])
    ], UIFiles);
    return UIFiles;
}());
export var UIFilesComponents = [UIFiles, UIPictures];

//# sourceMappingURL=ui-files.js.map
