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 };
    }
};
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { DndContext } from '@dnd-kit/core';
import { InlineAttributesPager } from './InlineAttributesPager';
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { debounce: function (x) { return x; } })); });
var simpleAttributeType = {
    uri: 'configuration/entityTypes/HCP/attributes/Simple',
    type: 'String',
    name: 'Simple',
    label: 'Simple label'
};
var crosswalks = [
    {
        uri: 'entities/sIeHttl/crosswalks/12jf9nL05',
        type: 'configuration/sources/Reltio',
        value: 'nQpZyQt',
        reltioLoadDate: '2021-06-11T08:24:31.726Z',
        createDate: '2018-11-19T12:14:00.735Z',
        updateDate: '2018-11-19T12:14:00.735Z',
        attributes: ['entities/sIeHttl/attributes/Simple/u123', 'entities/sIeHttl/attributes/Simple/u789'],
        singleAttributeUpdateDates: {
            'entities/sIeHttl/attributes/Date/1': '2018-11-29T05:39:56.445Z'
        }
    }
];
var defaultProps = {
    attributeType: simpleAttributeType,
    attributeValues: [],
    parentUri: 'entities/sIeHttl',
    crosswalksMap: {},
    onPin: jest.fn(),
    onIgnore: jest.fn(),
    onEdit: jest.fn(),
    onDelete: jest.fn(),
    onAdd: jest.fn(),
    emptyTempAttributeUris: []
};
var setUp = function (props) {
    if (props === void 0) { props = {}; }
    var user = userEvent.setup();
    var Providers = function (_a) {
        var children = _a.children;
        return React.createElement(DndContext, null, children);
    };
    return __assign({ user: user }, render(React.createElement(InlineAttributesPager, __assign({}, defaultProps, props)), { wrapper: Providers }));
};
describe('InlineAttributesPager tests', function () {
    describe('simple', function () {
        var crosswalksMap = {
            'entities/sIeHttl/attributes/Simple/u123': [crosswalks[0]],
            'entities/sIeHttl/attributes/Simple/u789': [crosswalks[0]]
        };
        var attributeValues = [
            {
                type: simpleAttributeType.uri,
                uri: 'entities/sIeHttl/attributes/Simple/u123',
                value: '123',
                ov: true
            },
            {
                type: simpleAttributeType.uri,
                uri: 'entities/sIeHttl/attributes/Simple/u789',
                value: '789',
                ov: false
            }
        ];
        it('should render simple attributes correctly', function () {
            setUp({ attributeType: simpleAttributeType, attributeValues: attributeValues, crosswalksMap: crosswalksMap });
            expect(screen.getByText('123')).toBeInTheDocument();
            expect(screen.getByText('789')).toBeInTheDocument();
        });
    });
    describe('nested', function () {
        var attributeType = {
            uri: 'configuration/entityTypes/HCP/attributes/Nested',
            type: 'Nested',
            name: 'Nested',
            label: 'Nested label',
            attributes: [
                {
                    uri: 'configuration/entityTypes/HCP/attributes/Nested/attributes/SubNested',
                    type: 'String',
                    name: 'SubNested',
                    label: 'SubNested label'
                }
            ]
        };
        var attributeValues = [
            {
                type: attributeType.uri,
                label: 'Attribute Label',
                uri: 'entities/sIeHttl/attributes/Nested/u123',
                value: {
                    SubNested: [
                        {
                            uri: 'entities/sIeHttl/attributes/Nested/u123/SubNested/1',
                            value: '1'
                        }
                    ]
                },
                ov: true
            },
            {
                type: attributeType.uri,
                label: 'Attribute Label 2',
                uri: 'entities/sIeHttl/attributes/Nested/u789',
                value: {
                    SubNested: [
                        {
                            uri: 'entities/sIeHttl/attributes/Nested/u789/SubNested/2',
                            value: '2'
                        }
                    ]
                },
                ov: false
            }
        ];
        it('should render nested attributes correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user, expandButtons;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        user = setUp({ attributeType: attributeType, attributeValues: attributeValues }).user;
                        expect(screen.getByText('Attribute Label')).toBeInTheDocument();
                        expect(screen.getByText('Attribute Label 2')).toBeInTheDocument();
                        expect(screen.queryByText('SubNested label')).not.toBeInTheDocument();
                        expandButtons = screen.getAllByTestId('arrow-expand-button');
                        return [4 /*yield*/, user.click(expandButtons[0])];
                    case 1:
                        _a.sent();
                        expect(screen.getByText('SubNested label')).toBeInTheDocument();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should render create button if values are empty', function () {
            setUp({ attributeType: attributeType, attributeValues: [] });
            expect(screen.getByText('Create attribute')).toBeInTheDocument();
        });
        it('should not render create button if values are empty but not editable', function () {
            setUp({ attributeType: attributeType, attributeValues: [], disableEdit: true });
            expect(screen.queryByText('Create attribute')).not.toBeInTheDocument();
        });
        it('should call onAdd on create button click', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        user = setUp({ attributeType: attributeType, attributeValues: [] }).user;
                        return [4 /*yield*/, user.click(screen.getByText('Create attribute'))];
                    case 1:
                        _a.sent();
                        expect(defaultProps.onAdd).toHaveBeenCalledWith({
                            parentUri: defaultProps.parentUri,
                            attributeType: attributeType,
                            index: 0
                        });
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe('reference', function () {
        it('should render reference attributes correctly', function () {
            var attributeType = {
                label: 'Address',
                name: 'Address',
                type: 'Reference',
                uri: 'configuration/entityTypes/HCP/attributes/Address',
                referencedAttributeURIs: ['configuration/entityTypes/Location/attributes/AddressLine1'],
                relationshipTypeURI: 'configuration/relationTypes/HasAddress'
            };
            var attributeValues = [
                {
                    label: 'Granada Aveue Santa Clara 1',
                    relationshipLabel: 'Office',
                    value: {
                        AddressLine1: [
                            {
                                type: 'configuration/entityTypes/Location/attributes/AddressLine1',
                                ov: true,
                                value: '3588 Granada Aveue',
                                uri: 'entities/1/attributes/Address/1/AddressLine1/1'
                            }
                        ]
                    },
                    ov: true,
                    uri: 'entities/1/attributes/Address/1',
                    refEntity: {
                        type: 'configuration/entityTypes/Location',
                        crosswalks: [
                            {
                                uri: 'entities/1/crosswalks/1YWdmjzf.3VKnDCovj',
                                type: 'configuration/sources/HMS',
                                value: 'cww1',
                                createDate: '2020-09-02T16:19:12.943Z',
                                updateDate: '2020-09-02T16:19:12.943Z',
                                attributeURIs: [
                                    'entities/1/attributes/Address/1/AddressLine1/1',
                                    'entities/1/attributes/Address/1'
                                ]
                            }
                        ],
                        objectURI: 'entities/1'
                    },
                    refRelation: {
                        type: 'configuration/relationTypes/HasAddress',
                        crosswalks: [
                            {
                                uri: 'entities/1/crosswalks/uAGtqeB.3VKnDCtBz',
                                type: 'configuration/sources/HMS',
                                value: 'cww2',
                                createDate: '2020-09-02T16:19:12.943Z',
                                updateDate: '2020-09-02T16:19:12.943Z',
                                attributeURIs: [
                                    'entities/1/attributes/Address/1/AddressLine/1',
                                    'entities/1/attributes/Address/1'
                                ]
                            }
                        ],
                        startRefPinned: false,
                        endRefPinned: false,
                        startRefIgnored: false,
                        endRefIgnored: false,
                        objectURI: 'relations/1'
                    },
                    startObjectCrosswalks: [
                        {
                            type: 'configuration/sources/HMS',
                            value: 'cww1'
                        }
                    ]
                },
                {
                    label: 'Granada Aveue Santa Clara 2',
                    relationshipLabel: 'Office',
                    value: {
                        AddressLine1: [
                            {
                                type: 'configuration/entityTypes/Location/attributes/AddressLine1',
                                ov: true,
                                value: '3587 Granada Aveue',
                                uri: 'entities/1/attributes/Address/2/AddressLine1/1'
                            }
                        ]
                    },
                    ov: true,
                    uri: 'entities/1/attributes/Address/2',
                    refEntity: {
                        type: 'configuration/entityTypes/Location',
                        crosswalks: [
                            {
                                uri: 'entities/1/crosswalks/1YWdmsWB.3VKnDDe4p',
                                type: 'configuration/sources/HMS',
                                value: 'cwf1',
                                createDate: '2020-09-02T16:19:12.943Z',
                                updateDate: '2020-09-02T16:19:12.943Z',
                                attributeURIs: [
                                    'entities/1/attributes/Address/2/AddressLine1/1',
                                    'entities/1/attributes/Address/2'
                                ]
                            }
                        ],
                        objectURI: 'entities/1'
                    },
                    refRelation: {
                        type: 'configuration/relationTypes/HasAddress',
                        crosswalks: [
                            {
                                uri: 'entities/1/crosswalks/uAGtuuR.3VKnDDiL5',
                                type: 'configuration/sources/HMS',
                                value: 'cwf2',
                                createDate: '2020-09-02T16:19:12.943Z',
                                updateDate: '2020-09-02T16:19:12.943Z',
                                attributeURIs: [
                                    'entities/1/attributes/Address/2/AddressLine1/1',
                                    'entities/1/attributes/Address/2'
                                ]
                            }
                        ],
                        startRefPinned: false,
                        endRefPinned: false,
                        startRefIgnored: false,
                        endRefIgnored: false,
                        objectURI: 'relations/2'
                    },
                    startObjectCrosswalks: [
                        {
                            type: 'configuration/sources/HMS',
                            value: 'cfw1'
                        }
                    ]
                }
            ];
            setUp({ attributeType: attributeType, attributeValues: attributeValues });
            expect(screen.getAllByTestId('inline-complex-attribute')).toHaveLength(2);
            expect(screen.getByText('Granada Aveue Santa Clara 1')).toBeInTheDocument();
            expect(screen.getByText('Granada Aveue Santa Clara 2')).toBeInTheDocument();
            expect(screen.getAllByText('Office')).toHaveLength(2);
        });
    });
    describe('image', function () {
        var crosswalks = [
            {
                uri: 'entities/j8K1vc9/crosswalks/12jf9nL05',
                type: 'configuration/sources/Reltio',
                value: 'nQpZyQt',
                reltioLoadDate: '2021-06-11T08:24:31.726Z',
                createDate: '2018-11-19T12:14:00.735Z',
                updateDate: '2018-11-19T12:14:00.735Z',
                attributes: ['configuration/entityTypes/Employee/attributes/ImageGallery/attributes/UrlThumbnail']
            }
        ];
        var attributeValues = [
            {
                type: 'configuration/entityTypes/Employee/attributes/ImageGallery',
                uri: 'entities/1Rd2I9Mt/attributes/ImageGallery/39V5GWJZv',
                value: {
                    UrlThumbnail: [
                        {
                            type: 'configuration/entityTypes/Employee/attributes/ImageGallery/attributes/UrlThumbnail',
                            ov: true,
                            value: '/test123.jpg',
                            uri: 'entities/1Rd2I9Mt/attributes/ImageGallery/39V5GWJZv/UrlThumbnail/5555'
                        }
                    ]
                },
                ov: true
            },
            {
                type: 'configuration/entityTypes/Employee/attributes/ImageGallery',
                uri: 'entities/1Rd2I9Mt/attributes/ImageGallery/123123',
                value: {
                    UrlThumbnail: [
                        {
                            type: 'configuration/entityTypes/Employee/attributes/ImageGallery/attributes/UrlThumbnail',
                            ov: true,
                            value: '/test.jpg',
                            uri: 'entities/1Rd2I9Mt/attributes/ImageGallery/123123/UrlThumbnail/66666'
                        }
                    ]
                },
                ov: true
            }
        ];
        var attributeType = {
            attributes: [
                {
                    name: 'UrlThumbnail',
                    type: 'String',
                    label: 'Thumbnail',
                    uri: 'configuration/entityTypes/Series/attributes/ImageGallery/attributes/UrlThumbnail'
                }
            ],
            name: 'ImageGallery',
            type: 'Image',
            uri: 'configuration/entityTypes/Employee/attributes/ImageGallery'
        };
        var crosswalksMap = {
            'entities/1Rd2I9Mt/attributes/ImageGallery/123123': [crosswalks[0]],
            'entities/1Rd2I9Mt/attributes/ImageGallery/39V5GWJZv': [crosswalks[0]]
        };
        it('should render image attributes correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user, images;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        user = setUp({ attributeType: attributeType, attributeValues: attributeValues, crosswalksMap: crosswalksMap }).user;
                        images = screen.getAllByRole('img');
                        expect(images).toHaveLength(2);
                        expect(images[0]).toHaveAttribute('src', '/test123.jpg');
                        expect(images[1]).toHaveAttribute('src', '/test.jpg');
                        expect(screen.getAllByTestId('inline-complex-attribute')).toHaveLength(2);
                        expect(screen.getAllByTestId('arrow-expand-button')).toHaveLength(2);
                        return [4 /*yield*/, user.hover(images[0])];
                    case 1:
                        _a.sent();
                        expect(screen.getByLabelText('Pin')).toBeInTheDocument();
                        expect(screen.getByLabelText('Delete')).toBeInTheDocument();
                        expect(screen.getByLabelText('Ignore')).toBeInTheDocument();
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe('paging', function () {
        var attributeType = {
            uri: 'configuration/entityTypes/HCP/attributes/Simple',
            type: 'String',
            name: 'Simple',
            label: 'Simple label'
        };
        var attributeValues = Array.from({ length: 5 }, function (_, i) { return ({
            type: attributeType.uri,
            uri: "entities/sIeHttl/attributes/Simple/u".concat(i + 1),
            value: "u".concat(i + 1),
            ov: i === 0
        }); });
        var crosswalksMap = attributeValues.reduce(function (acc, value) {
            acc[value.uri] = [crosswalks[0]];
            return acc;
        }, {});
        it('should show Show more button if has more attributes to show', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        user = setUp({
                            crosswalksMap: crosswalksMap,
                            attributeType: attributeType,
                            attributeValues: attributeValues,
                            paging: { totalValues: 5 },
                            max: 2
                        }).user;
                        expect(screen.getByText('u1')).toBeInTheDocument();
                        expect(screen.getByText('u2')).toBeInTheDocument();
                        expect(screen.queryByText('u3')).not.toBeInTheDocument();
                        expect(screen.queryByText('u4')).not.toBeInTheDocument();
                        expect(screen.queryByText('u5')).not.toBeInTheDocument();
                        expect(screen.queryByText('Show less')).not.toBeInTheDocument();
                        return [4 /*yield*/, user.click(screen.getByText('Show 2 more of remaining 3 value'))];
                    case 1:
                        _a.sent();
                        expect(screen.queryByText('Show less')).not.toBeInTheDocument();
                        expect(screen.getByText('u3')).toBeInTheDocument();
                        expect(screen.getByText('u4')).toBeInTheDocument();
                        expect(screen.queryByText('u5')).not.toBeInTheDocument();
                        return [4 /*yield*/, user.click(screen.getByText('Show 1 more of remaining 1 value'))];
                    case 2:
                        _a.sent();
                        expect(screen.getByText('Show less')).toBeInTheDocument();
                        expect(screen.getByText('u5')).toBeInTheDocument();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should show Show less button if has no more attributes to show', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        user = setUp({
                            crosswalksMap: crosswalksMap,
                            attributeType: attributeType,
                            attributeValues: attributeValues,
                            paging: { totalValues: 5 },
                            max: 2
                        }).user;
                        return [4 /*yield*/, user.click(screen.getByText('Show 2 more of remaining 3 value'))];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, user.click(screen.getByText('Show 1 more of remaining 1 value'))];
                    case 2:
                        _a.sent();
                        return [4 /*yield*/, user.click(screen.getByText('Show less'))];
                    case 3:
                        _a.sent();
                        expect(screen.getByText('u1')).toBeInTheDocument();
                        expect(screen.getByText('u2')).toBeInTheDocument();
                        expect(screen.queryByText('u3')).not.toBeInTheDocument();
                        expect(screen.queryByText('u4')).not.toBeInTheDocument();
                        expect(screen.queryByText('u5')).not.toBeInTheDocument();
                        expect(screen.queryByText('Show less')).not.toBeInTheDocument();
                        expect(screen.getByText('Show 2 more of remaining 3 value')).toBeInTheDocument();
                        return [2 /*return*/];
                }
            });
        }); });
    });
});
