"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.DCRImageEditor = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var CloudUpload_1 = __importDefault(require("@mui/icons-material/CloudUpload"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var SmallIconButton_1 = require("../../../../../SmallIconButton");
var UploadImageDialog_1 = require("../../../../../UploadImageDialog");
var DCRImageEditor = function (_a) {
    var change = _a.change, onUpload = _a.onUpload, disabled = _a.disabled;
    var attributeType = change.attributeType;
    var isDeleted = change.operation === mdm_sdk_1.DCROperationTypes.DELETED;
    var thumbnailUrl = (0, react_1.useMemo)(function () {
        var _a, _b;
        var innerChange = (_a = change.attributeValue) === null || _a === void 0 ? void 0 : _a.value;
        var imageSource = (_b = innerChange === null || innerChange === void 0 ? void 0 : innerChange.newValue) !== null && _b !== void 0 ? _b : change.attributeValue;
        return (0, mdm_sdk_1.getImageAttributeThumbnailUrl)((imageSource !== null && imageSource !== void 0 ? imageSource : {}));
    }, [change.attributeValue]);
    var _b = (0, react_1.useState)(false), isDialogOpen = _b[0], setIsDialogOpen = _b[1];
    var _c = (0, react_1.useState)(null), uploadedThumbnailUrl = _c[0], setUploadedThumbnailUrl = _c[1];
    var toggleDialog = (0, react_1.useCallback)(function () {
        setIsDialogOpen(function (prev) { return !prev; });
    }, []);
    var handleUpload = (0, react_1.useCallback)(function (image) {
        var imageValue = {};
        var subAttributes = (attributeType === null || attributeType === void 0 ? void 0 : attributeType.attributes) || [];
        var _loop_1 = function (key) {
            if (subAttributes.some(function (_a) {
                var name = _a.name;
                return name === key;
            })) {
                imageValue[key] = [{ value: image[key] }];
            }
        };
        for (var key in image) {
            _loop_1(key);
        }
        onUpload({ value: imageValue });
        setUploadedThumbnailUrl(image.CdnUrlThumbnail || image.CdnUrl || null);
    }, [attributeType, onUpload]);
    var isDisabled = disabled || isDeleted;
    var displayUrl = uploadedThumbnailUrl || thumbnailUrl;
    return (react_1.default.createElement(react_1.default.Fragment, null,
        displayUrl ? (react_1.default.createElement("img", { src: displayUrl, alt: "", style: {
                maxWidth: 80,
                maxHeight: 80,
                objectFit: 'cover',
                cursor: isDisabled ? 'default' : 'pointer'
            }, onClick: isDisabled ? undefined : toggleDialog })) : (react_1.default.createElement(SmallIconButton_1.SmallIconButton, { icon: CloudUpload_1.default, onClick: toggleDialog, title: ui_i18n_1.default.text('Upload image'), disabled: isDisabled })),
        react_1.default.createElement(UploadImageDialog_1.UploadImageDialog, { open: isDialogOpen, onUpload: handleUpload, onClose: toggleDialog })));
};
exports.DCRImageEditor = DCRImageEditor;
