"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.UploadImageDialog = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Box_1 = __importDefault(require("@mui/material/Box"));
var Dialog_1 = __importDefault(require("@mui/material/Dialog"));
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var LinearLoadIndicator_1 = require("../LinearLoadIndicator");
var ImageByUrlField_1 = require("./components/ImageByUrlField");
var TargetBox_1 = require("./components/TargetBox");
var DividerWithText_1 = require("./components/DividerWithText");
var ErrorSnackbar_1 = require("./components/ErrorSnackbar");
var constants_1 = require("./constants");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var styles_1 = require("./styles");
var UploadImageDialog = function (_a) {
    var open = _a.open, onClose = _a.onClose, onUpload = _a.onUpload;
    var styles = (0, styles_1.useStyles)();
    var _b = (0, react_1.useState)(false), loading = _b[0], setLoading = _b[1];
    var _c = (0, react_1.useState)(null), error = _c[0], setError = _c[1];
    var imageServicePath = (0, MdmModuleContext_1.useMdmImageServicePath)();
    var environment = (0, MdmModuleContext_1.useMdmEnvironment)();
    var tenant = (0, MdmModuleContext_1.useMdmTenant)();
    var uploadImage = function (image) {
        setLoading(true);
        (0, mdm_sdk_1.uploadImage)({ image: image, imageServicePath: imageServicePath, environment: environment, tenant: tenant })
            .then(function (res) {
            onUpload(res);
            onClose();
        })
            .catch(function (err) {
            setError((0, mdm_sdk_1.getRequestErrorMessage)(err, ui_i18n_1.default.text('Something went wrong')));
        })
            .finally(function () { return setLoading(false); });
    };
    var handleAppendImage = function (image) {
        if (!image) {
            return setError(ui_i18n_1.default.text('No image selected.'));
        }
        var size = Number((image.size / 1024 / 1024).toFixed(2));
        var isCorrectType = constants_1.IMAGE_TYPES.includes(image.type);
        if (size > constants_1.MAX_IMAGE_SIZE) {
            setError(ui_i18n_1.default.text('File size exceeds configured max file size.'));
        }
        else if (!isCorrectType) {
            setError(ui_i18n_1.default.text('File format is not supported.'));
        }
        else {
            uploadImage(image);
        }
    };
    var handleSendImage = function (url) {
        uploadImage(url);
    };
    return (react_1.default.createElement(Dialog_1.default, { open: open, onClose: onClose, classes: { paper: styles.dialogPaper } },
        react_1.default.createElement(Typography_1.default, { className: styles.title }, ui_i18n_1.default.text('Add new image')),
        react_1.default.createElement(Box_1.default, { className: styles.body },
            react_1.default.createElement(TargetBox_1.TargetBox, { className: styles.targetBox, onAppendFile: handleAppendImage }),
            react_1.default.createElement(DividerWithText_1.DividerWithText, { className: styles.divider }, ui_i18n_1.default.text('or')),
            react_1.default.createElement(ImageByUrlField_1.ImageByUrlField, { onSend: handleSendImage })),
        loading && react_1.default.createElement(LinearLoadIndicator_1.LinearLoadIndicator, null),
        react_1.default.createElement(ErrorSnackbar_1.ErrorSnackbar, { error: error, onClose: function () { return setError(null); } })));
};
exports.UploadImageDialog = UploadImageDialog;
