var _uploadId, _upload;
import { __awaiter, __classPrivateFieldGet } from "tslib";
import { Injectable } from '@angular/core';
import { ZuiF, ZuiFile, ZuiN, ZuiS } from '@material-zui/core';
import * as $ from 'jquery';
import { ZUIToastV2Service } from '../toast-v2';
import { getConfig } from './data';
export class ZuiUploadService {
    constructor(zts) {
        this.zts = zts;
        _uploadId.set(this, ZuiF.uuid());
        _upload.set(this, (option, isMutiple = true) => new Promise(resolve => {
            const uploadId = `#${__classPrivateFieldGet(this, _uploadId)}`;
            const multiple = isMutiple ? 'multiple' : '';
            if ($(`${uploadId}`).length)
                $('body').children(uploadId).remove();
            $('body').append(`<input id="${__classPrivateFieldGet(this, _uploadId)}" style="display: none" type="file" ${multiple} />`);
            const uploadEl = $(`${uploadId}`);
            uploadEl.on('change', (evt) => {
                const valid = [];
                const inValid = [];
                const target = evt.target;
                const { files } = target;
                // const {extensions = [], maxSize = {}, validator, max: maxLength, showError = false} = option ?? {}
                const { extensions = [], maxSize = {}, validator, max: maxLength, showError = false } = getConfig(option);
                const { byte = 0, Kb = 0, Mb = 0 } = maxSize;
                const max = byte + Kb * 1024 + Mb * 1024 * 1024;
                Array.from(files).forEach((file, i) => {
                    const { name, size } = file;
                    const { extension } = ZuiFile.splitFileName(name);
                    let isValid = true;
                    let message = '';
                    if (extensions.length && !extensions.includes(extension)) {
                        message = `Extension ".${extension}" is not support`;
                        isValid = false;
                    }
                    if (max > 0 && size > max) {
                        message = `File size ${ZuiN.format(size)} byte is not valid`;
                        isValid = false;
                    }
                    if (validator) {
                        const result = validator(file);
                        if (ZuiS.isString(result)) {
                            message = result;
                            isValid = false;
                        }
                    }
                    if (maxLength && maxLength < valid.length + 1) {
                        message = `Max file allow upload is ${maxLength}`;
                        isValid = false;
                    }
                    if (isValid) {
                        valid.push(file);
                    }
                    else {
                        inValid.push({ file, message });
                    }
                });
                if (showError && inValid.length)
                    this.zts.error(inValid.map(item => `- File "${item.file.name}": ${item.message}`));
                resolve({ valid, inValid });
            });
            uploadEl.trigger('click');
        }));
        this.uploadFile = (option) => __awaiter(this, void 0, void 0, function* () {
            const { valid, inValid } = yield __classPrivateFieldGet(this, _upload).call(this, option, false);
            const file = valid === null || valid === void 0 ? void 0 : valid[0];
            const isValid = !inValid.length;
            return { file, isValid };
        });
        this.uploadFiles = (option) => __classPrivateFieldGet(this, _upload).call(this, option);
        this.uploadExcel = (option) => __awaiter(this, void 0, void 0, function* () {
            const { valid, inValid } = yield __classPrivateFieldGet(this, _upload).call(this, option, false);
            const file = valid === null || valid === void 0 ? void 0 : valid[0];
            const isValid = !inValid.length;
            return { file, isValid };
        });
    }
}
_uploadId = new WeakMap(), _upload = new WeakMap();
ZuiUploadService.decorators = [
    { type: Injectable }
];
ZuiUploadService.ctorParameters = () => [
    { type: ZUIToastV2Service }
];
//# sourceMappingURL=index.service.js.map