"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var jsdom_testing_mocks_1 = require("jsdom-testing-mocks");
var react_2 = require("@testing-library/react");
var user_event_1 = __importDefault(require("@testing-library/user-event"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var styles_1 = require("@mui/styles");
var styles_2 = require("@mui/material/styles");
var constants_1 = require("../constants");
var helpers_1 = require("../ImageAttributesLine/helpers");
var imageAttributeValues_test_data_1 = require("../ImageAttributesLine/imageAttributeValues.test-data");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var ImageAttributesLineEditor_1 = require("./ImageAttributesLineEditor");
jest.unmock('@mui/styles');
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { insertDefaultImageByAttrUri: jest.fn().mockReturnValue({ then: function (f) { return f({ status: 'success' }); } }), uploadImage: jest.fn() })); });
jest.mock('../ImageAttributesLine/helpers', function () { return ({
    downloadImagesByURLs: jest.fn()
}); });
var entity = { uri: 'entities/1Rd2I9Mt' };
var modifiedEntities = {
    'entities/1Rd2I9Mt': {
        defaultProfilePic: 'entities/1Rd2I9Mt/attributes/ImageGallery/39V5GWJZv',
        uri: 'entities/01eLeVm',
        type: 'configuration/entityTypes/HCP'
    }
};
var defaultMdmValues = {
    entity: entity,
    modifiedEntities: modifiedEntities,
    mode: mdm_sdk_1.Mode.Viewing,
    imageServicePath: '',
    environment: '',
    tenant: { id: 'tenant', name: 'tenant' }
};
var defaultMdmActions = {
    setDefaultProfilePicForModifiedEntity: jest.fn(),
    setDefaultProfilePicForEntity: jest.fn()
};
var onChangeAttribute = jest.fn();
var onDeleteAttribute = jest.fn();
var defaultProps = {
    attributeValues: imageAttributeValues_test_data_1.imageAttributeValues,
    attributeType: imageAttributeValues_test_data_1.attributeType,
    parentUri: 'entities/01eLeVm',
    onDeleteAttribute: onDeleteAttribute,
    onChangeAttribute: onChangeAttribute
};
var setUp = function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.props, props = _c === void 0 ? {} : _c, _d = _b.mdmValues, mdmValues = _d === void 0 ? {} : _d, _e = _b.mdmActions, mdmActions = _e === void 0 ? {} : _e;
    var user = user_event_1.default.setup({ skipHover: true });
    var Providers = function (_a) {
        var children = _a.children;
        return (react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: __assign(__assign({}, defaultMdmValues), mdmValues), actions: __assign(__assign({}, defaultMdmActions), mdmActions) },
            react_1.default.createElement(styles_1.ThemeProvider, { theme: (0, styles_2.createTheme)(constants_1.theme) }, children)));
    };
    return __assign({ user: user }, (0, react_2.render)(react_1.default.createElement(ImageAttributesLineEditor_1.ImageAttributesLineEditor, __assign({}, defaultProps, props)), { wrapper: Providers }));
};
describe('ImageAttributesLineEditor edit tests', function () {
    var resizeObserver = (0, jsdom_testing_mocks_1.mockResizeObserver)();
    beforeEach(function () {
        jest.clearAllMocks();
    });
    it('should render main parts', function () {
        setUp();
        var container = react_2.screen.getByTestId('image-line-edit-mode');
        resizeObserver.mockElementSize(container, { contentBoxSize: { inlineSize: 600 } });
        (0, react_2.act)(function () {
            resizeObserver.resize();
        });
        expect(react_2.screen.getByTestId('reltio-upload-image-button')).toBeInTheDocument();
        var images = react_2.screen.getAllByRole('img', { hidden: true });
        expect(images).toHaveLength(3);
    });
    it('should provide custom sizeImage to Image and ImageAttributesLine', function () {
        var imageSize = { imageMargin: 40, imageWidth: 11, imageHeight: 3 };
        setUp({ props: { imageSize: imageSize } });
        var container = react_2.screen.getByTestId('image-line-edit-mode');
        resizeObserver.mockElementSize(container, { contentBoxSize: { inlineSize: 300 } });
        (0, react_2.act)(function () {
            resizeObserver.resize();
        });
        var images = react_2.screen.getAllByRole('img', { hidden: true });
        expect(images).toHaveLength(3);
        var computedStyles = getComputedStyle(images[0]);
        expect(computedStyles.width).toBe('11px');
        expect(computedStyles.height).toBe('3px');
    });
    it('should render images', function () {
        setUp({ props: { attributeValues: imageAttributeValues_test_data_1.imageAttributeValues } });
        var container = react_2.screen.getByTestId('image-line-edit-mode');
        resizeObserver.mockElementSize(container, { contentBoxSize: { inlineSize: 600 } });
        (0, react_2.act)(function () {
            resizeObserver.resize();
        });
        var images = react_2.screen.getAllByRole('img', { hidden: true });
        expect(images).toHaveLength(3);
        expect(images[0]).toHaveAttribute('src', '/test.jpg');
        expect(images[1]).toHaveAttribute('src', '/test2.jpg');
        expect(images[2]).toHaveAttribute('src', '/wrong.jpg');
    });
    it('should open gallery dialog on image overlay click', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, container;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp({ props: { attributeValues: imageAttributeValues_test_data_1.imageAttributeValues } }).user;
                    expect(react_2.screen.queryByRole('dialog')).not.toBeInTheDocument();
                    container = react_2.screen.getByTestId('image-line-edit-mode');
                    resizeObserver.mockElementSize(container, { contentBoxSize: { inlineSize: 600 } });
                    (0, react_2.act)(function () {
                        resizeObserver.resize();
                    });
                    return [4 /*yield*/, user.click(react_2.screen.getAllByRole('img', { hidden: true })[0])];
                case 1:
                    _a.sent();
                    expect(react_2.screen.getByRole('dialog')).toBeInTheDocument();
                    expect(react_2.screen.getByTestId('thumbnails-container').childNodes).toHaveLength(3);
                    expect(react_2.screen.getByText('Gallery')).toBeInTheDocument();
                    expect(react_2.screen.getByText('Meta info')).toBeInTheDocument();
                    expect(react_2.screen.getAllByTestId('image-meta-info-item')[0].querySelector('a')).toHaveAttribute('href', '/test.jpg');
                    return [4 /*yield*/, user.click(react_2.screen.getByLabelText('Close'))];
                case 2:
                    _a.sent();
                    return [4 /*yield*/, (0, react_2.waitForElementToBeRemoved)(react_2.screen.queryByRole('dialog'))];
                case 3:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call image overlay actions', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, container, clipboardText;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp({ props: { attributeValues: imageAttributeValues_test_data_1.imageAttributeValues, overlay: true } }).user;
                    container = react_2.screen.getByTestId('image-line-edit-mode');
                    resizeObserver.mockElementSize(container, { contentBoxSize: { inlineSize: 600 } });
                    (0, react_2.act)(function () {
                        resizeObserver.resize();
                    });
                    react_2.fireEvent.load(react_2.screen.getAllByRole('img', { hidden: true })[0]);
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('image-actions-overlay'))];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('download-button'))];
                case 2:
                    _a.sent();
                    expect(helpers_1.downloadImagesByURLs).toHaveBeenCalledWith(['/test.jpg']);
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('image-overlay-actions'))];
                case 3:
                    _a.sent();
                    return [4 /*yield*/, user.click(react_2.screen.getByText('Share link'))];
                case 4:
                    _a.sent();
                    return [4 /*yield*/, navigator.clipboard.readText()];
                case 5:
                    clipboardText = _a.sent();
                    expect(clipboardText).toBe('/test.jpg');
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('image-overlay-actions'))];
                case 6:
                    _a.sent();
                    return [4 /*yield*/, user.click(react_2.screen.getByText('Set as default'))];
                case 7:
                    _a.sent();
                    expect(mdm_sdk_1.insertDefaultImageByAttrUri).toHaveBeenCalledWith('entities/1Rd2I9Mt', imageAttributeValues_test_data_1.imageAttributeValues[0].uri);
                    expect(defaultMdmActions.setDefaultProfilePicForEntity).toHaveBeenCalledWith(imageAttributeValues_test_data_1.imageAttributeValues[0]);
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('image-overlay-actions'))];
                case 8:
                    _a.sent();
                    return [4 /*yield*/, user.click(react_2.screen.getByText('Delete'))];
                case 9:
                    _a.sent();
                    expect(onDeleteAttribute).toHaveBeenCalledWith({
                        uri: imageAttributeValues_test_data_1.imageAttributeValues[0].uri,
                        attributeType: defaultProps.attributeType
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should open and close UploadImageDialog by clicking on .uploadContainer', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, backdrop;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    expect(react_2.screen.queryByRole('dialog')).not.toBeInTheDocument();
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('reltio-upload-image-button'))];
                case 1:
                    _a.sent();
                    expect(react_2.screen.getByRole('dialog')).toBeInTheDocument();
                    expect(react_2.screen.getByText('Add new image')).toBeInTheDocument();
                    backdrop = react_2.screen.getAllByRole('presentation')[0].firstChild;
                    return [4 /*yield*/, user.click(backdrop)];
                case 2:
                    _a.sent();
                    return [4 /*yield*/, (0, react_2.waitForElementToBeRemoved)(react_2.screen.queryByRole('dialog'))];
                case 3:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); });
    describe('onChangeAttribute test', function () {
        it('should add images to globalState with imageProps', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user, uploadImageDialog;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        mdm_sdk_1.uploadImage.mockResolvedValue({
                            Url: 'https://s3.amazonaws.com/reltio.qa.ih/sourcenew/hc3IZh8C1ixGWpmRPr54HufUd.png',
                            UrlThumbnail: 'https://s3.amazonaws.com/reltio.qa.ih/thumbnailInew/oLd1Fs5Sh2SVHHHV7uyHFZhsi.png',
                            CdnUrlThumbnail: 'https://s3.amazonaws.com/reltio.qa.ih/thumbnailInew/oLd1Fs5Sh2SVHHHV7uyHFZhsi.png',
                            Width: '990',
                            Height: '946',
                            Size: '114373',
                            MimeType: 'image/png',
                            CreationTimestamp: 1734017226633
                        });
                        user = setUp().user;
                        return [4 /*yield*/, user.click(react_2.screen.getByTestId('reltio-upload-image-button'))];
                    case 1:
                        _a.sent();
                        expect(react_2.screen.getByRole('dialog')).toBeInTheDocument();
                        uploadImageDialog = react_2.screen.getByTestId('upload-image-button');
                        expect(uploadImageDialog).toBeInTheDocument();
                        return [4 /*yield*/, user.upload(uploadImageDialog, new File(['test'], 'test.png', { type: 'image/png' }))];
                    case 2:
                        _a.sent();
                        expect(onChangeAttribute).toHaveBeenCalledWith({
                            attributeType: imageAttributeValues_test_data_1.attributeType,
                            uri: expect.any(String),
                            value: {
                                CdnUrlThumbnail: [
                                    {
                                        ov: true,
                                        type: 'configuration/entityTypes/Series/attributes/ImageGallery/attributes/CdnUrlThumbnail',
                                        uri: expect.any(String),
                                        value: 'https://s3.amazonaws.com/reltio.qa.ih/thumbnailInew/oLd1Fs5Sh2SVHHHV7uyHFZhsi.png'
                                    }
                                ],
                                Url: [
                                    {
                                        ov: true,
                                        type: 'configuration/entityTypes/Series/attributes/ImageGallery/attributes/Url',
                                        uri: expect.any(String),
                                        value: 'https://s3.amazonaws.com/reltio.qa.ih/sourcenew/hc3IZh8C1ixGWpmRPr54HufUd.png'
                                    }
                                ],
                                UrlThumbnail: [
                                    {
                                        ov: true,
                                        type: 'configuration/entityTypes/Series/attributes/ImageGallery/attributes/UrlThumbnail',
                                        uri: expect.any(String),
                                        value: 'https://s3.amazonaws.com/reltio.qa.ih/thumbnailInew/oLd1Fs5Sh2SVHHHV7uyHFZhsi.png'
                                    }
                                ]
                            }
                        });
                        return [4 /*yield*/, (0, react_2.waitForElementToBeRemoved)(react_2.screen.queryByRole('dialog'))];
                    case 3:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe('handleDeleteAttribute test', function () {
        it('should delete and choose another defaultPic', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user, container;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        user = setUp({ props: { attributeValues: imageAttributeValues_test_data_1.imageAttributeValues, overlay: true } }).user;
                        container = react_2.screen.getByTestId('image-line-edit-mode');
                        resizeObserver.mockElementSize(container, { contentBoxSize: { inlineSize: 600 } });
                        (0, react_2.act)(function () {
                            resizeObserver.resize();
                        });
                        react_2.fireEvent.load(react_2.screen.getAllByRole('img', { hidden: true })[0]);
                        return [4 /*yield*/, user.hover(react_2.screen.getByTestId('image-actions-overlay'))];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, user.click(react_2.screen.getByTestId('image-overlay-actions'))];
                    case 2:
                        _a.sent();
                        return [4 /*yield*/, user.click(react_2.screen.getByText('Delete'))];
                    case 3:
                        _a.sent();
                        expect(defaultMdmActions.setDefaultProfilePicForModifiedEntity).toHaveBeenCalledWith({
                            entityUri: 'entities/1Rd2I9Mt',
                            value: imageAttributeValues_test_data_1.imageAttributeValues[1]
                        });
                        expect(onDeleteAttribute).toHaveBeenCalledWith({
                            uri: imageAttributeValues_test_data_1.imageAttributeValues[0].uri,
                            attributeType: defaultProps.attributeType
                        });
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not choose another defaultPic if non-default deleted', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user, container;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        user = setUp({ props: { attributeValues: imageAttributeValues_test_data_1.imageAttributeValues, overlay: true } }).user;
                        container = react_2.screen.getByTestId('image-line-edit-mode');
                        resizeObserver.mockElementSize(container, { contentBoxSize: { inlineSize: 600 } });
                        (0, react_2.act)(function () {
                            resizeObserver.resize();
                        });
                        react_2.fireEvent.load(react_2.screen.getAllByRole('img', { hidden: true })[1]);
                        return [4 /*yield*/, user.hover(react_2.screen.getByTestId('image-actions-overlay'))];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, user.click(react_2.screen.getByTestId('image-overlay-actions'))];
                    case 2:
                        _a.sent();
                        return [4 /*yield*/, user.click(react_2.screen.getByText('Delete'))];
                    case 3:
                        _a.sent();
                        expect(defaultMdmActions.setDefaultProfilePicForModifiedEntity).not.toHaveBeenCalled();
                        expect(onDeleteAttribute).toHaveBeenCalledWith({
                            uri: imageAttributeValues_test_data_1.imageAttributeValues[1].uri,
                            attributeType: defaultProps.attributeType
                        });
                        return [2 /*return*/];
                }
            });
        }); });
    });
});
