"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UploadFileButton = void 0;
var react_1 = __importStar(require("react"));
var CircularProgress_1 = __importDefault(require("@mui/material/CircularProgress"));
var Link_1 = __importDefault(require("@mui/material/Link"));
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var Cancel_1 = __importDefault(require("@mui/icons-material/Cancel"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ExpandedValueTooltip_1 = require("../ExpandedValueTooltip");
var UploadMode_1 = require("./components/UploadMode");
var helpers_1 = require("./helpers");
var constants_1 = require("./constants");
var styles_1 = require("./styles");
var UploadFileButton = function (_a) {
    var link = _a.link, filename = _a.filename, onUpload = _a.onUpload, _b = _a.onError, onError = _b === void 0 ? helpers_1.showFileUploadError : _b, onCancel = _a.onCancel, supportedFileTypes = _a.supportedFileTypes, _c = _a.isValidLink, isValidLink = _c === void 0 ? true : _c;
    var _d = (0, react_1.useState)(constants_1.UploadState.Init), mode = _d[0], setMode = _d[1];
    var _e = (0, react_1.useState)(''), name = _e[0], setName = _e[1];
    var styles = (0, styles_1.useStyles)();
    (0, react_1.useEffect)(function () {
        setName(filename);
    }, [filename]);
    (0, react_1.useEffect)(function () {
        setMode(link ? constants_1.UploadState.Uploaded : constants_1.UploadState.Init);
    }, [link]);
    var onErrorHandler = (0, react_1.useCallback)(function (error) {
        setMode(constants_1.UploadState.Init);
        onError(error);
    }, [onError]);
    var onUploadHandler = (0, react_1.useCallback)(function (result) {
        return onUpload(result)
            .then(function () {
            setMode(constants_1.UploadState.Uploaded);
            setName(result.name);
        })
            .catch(onErrorHandler);
    }, [onUpload, onErrorHandler]);
    var onBeforeUpload = (0, react_1.useCallback)(function () {
        setMode(constants_1.UploadState.Uploading);
    }, []);
    var switchToInitMode = (0, react_1.useCallback)(function () {
        setMode(constants_1.UploadState.Init);
        onCancel();
    }, [onCancel]);
    switch (mode) {
        case constants_1.UploadState.Init:
            return (react_1.default.createElement(UploadMode_1.UploadMode, { supportedFileTypes: supportedFileTypes, onUpload: onUploadHandler, onBeforeUpload: onBeforeUpload }));
        case constants_1.UploadState.Uploaded: {
            var label = (0, mdm_sdk_1.getLabel)(name);
            return (react_1.default.createElement("div", { className: styles.uploaded },
                react_1.default.createElement(ExpandedValueTooltip_1.ExpandedValueTooltip, { value: label }, isValidLink ? (react_1.default.createElement(Link_1.default, { href: link, className: styles.label, underline: "hover" }, label)) : (react_1.default.createElement(Typography_1.default, { variant: "inherit", className: styles.label }, label))),
                react_1.default.createElement(Cancel_1.default, { onClick: switchToInitMode, onMouseDown: switchToInitMode, onTouchEnd: switchToInitMode, className: styles.cancelIcon })));
        }
        case constants_1.UploadState.Uploading:
            return react_1.default.createElement(CircularProgress_1.default, { size: constants_1.PROGRESS_ICON_SIZE });
        default:
            return null;
    }
};
exports.UploadFileButton = UploadFileButton;
