import { __assign, __extends } from "tslib";
import { PureComponent } from 'react';
import { FILE_UPLOAD_STATUS } from '../constants';
var AbstractUpload = (function (_super) {
    __extends(AbstractUpload, _super);
    function AbstractUpload() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.emitOnError = function (type, data) {
            _this.props.onError && _this.props.onError(type, data);
        };
        _this.emitOnUpload = function (file, uploadItem) {
            var _a = _this.props, onUpload = _a.onUpload, manualUpload = _a.manualUpload;
            var uploadItemId = uploadItem.id;
            if (!manualUpload && onUpload) {
                onUpload(file, _this.updateUploadItemPercent.bind(_this, uploadItemId))
                    .then(function (onUploadReturn) {
                    _this.updateUploadItemStatusToSuccess(uploadItemId, onUploadReturn);
                })
                    .catch(function () {
                    _this.updateUploadItemStatusToFailed(uploadItemId);
                });
            }
        };
        _this.updateUploadItemStatusToSuccess = function (updateItemId, onUploadSuccessReturn) {
            var overrideProps = __assign({ status: FILE_UPLOAD_STATUS.success }, _this.getUploadSuccessOverrideProps(onUploadSuccessReturn));
            _this.updateUploadItem(updateItemId, overrideProps);
        };
        _this.updateUploadItemStatusToFailed = function (updateItemId) {
            var overrideProps = {
                status: FILE_UPLOAD_STATUS.failed,
            };
            _this.updateUploadItem(updateItemId, overrideProps);
        };
        _this.updateUploadItemPercent = function (updateItemId, percent) {
            var updateItem = _this.getUploadItem(updateItemId);
            if (!updateItem ||
                updateItem.status === FILE_UPLOAD_STATUS.success ||
                updateItem.status === FILE_UPLOAD_STATUS.failed) {
                return;
            }
            var overrideProps = {
                status: FILE_UPLOAD_STATUS.uploading,
                percent: percent,
            };
            _this.updateUploadItem(updateItemId, overrideProps);
        };
        return _this;
    }
    AbstractUpload.prototype.getUploadSuccessOverrideProps = function (onUploadSuccessReturn) {
        return onUploadSuccessReturn;
    };
    return AbstractUpload;
}(PureComponent));
export default AbstractUpload;
