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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import React from 'react';
import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { EntityAttrTypes } from '@reltio/mdm-sdk';
import ReadOnlyAttributesList from './ReadOnlyAttributesList';
import { attributesList } from './attributes.test-data';
import { MdmModuleProvider } from '../contexts/MdmModuleContext';
var defaultEntity = {
    type: 'configuration/entityTypes/HCP',
    uri: 'entities/xT3HJZ7',
    attributes: {
        Role: [
            {
                ov: true,
                type: 'configuration/entityTypes/HCP/attributes/Role',
                uri: 'entities/xT3HJZ7/attributes/Role/nAs391Ab',
                value: 'Some role 1'
            },
            {
                ov: true,
                type: 'configuration/entityTypes/HCP/attributes/Role',
                uri: 'entities/xT3HJZ7/attributes/Role/8AmySE1',
                value: 'Some role 2'
            }
        ],
        Name: [
            {
                ov: true,
                type: 'configuration/entityTypes/HCP/attributes/Name',
                uri: 'entities/xT3HJZ7/attributes/Name/8Ans02G',
                value: 'Some name'
            }
        ],
        LastName: [
            {
                ov: true,
                type: 'configuration/entityTypes/HCP/attributes/LastName',
                uri: 'entities/xT3HJZ7/attributes/LastName/pZXO45h',
                value: 'Some last name'
            }
        ],
        paging: {
            'configuration/entityTypes/HCP/attributes/Role': {
                totalOvValues: 4,
                totalValues: 4
            }
        }
    }
};
var defaultMetadata = {
    entityTypes: [{ uri: 'configuration/entityTypes/HCP', label: 'HCP', attributes: attributesList }]
};
var defaultProps = {
    entity: defaultEntity,
    attrTypes: attributesList,
    parentUri: 'entities/xT3HJZ7'
};
var defaultMdmValues = {
    metadata: defaultMetadata,
    entity: defaultEntity
};
var setUp = function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.props, props = _c === void 0 ? defaultProps : _c, _d = _b.mdmValues, mdmValues = _d === void 0 ? defaultMdmValues : _d;
    var user = userEvent.setup();
    var Providers = function (_a) {
        var children = _a.children;
        return (React.createElement(MdmModuleProvider, { values: mdmValues }, children));
    };
    return __assign(__assign({}, render(React.createElement(ReadOnlyAttributesList, __assign({}, props)), { wrapper: Providers })), { user: user });
};
describe('Read only attributes list tests', function () {
    it('should render pagers for not hidden attribute types and for existing attributes', function () {
        setUp();
        var multiLineRenderer = screen.getByTestId('multi-line-renderer');
        var multiLineRendererLabel = within(multiLineRenderer).getByTestId('reltio-attribute-label');
        var multiLineRendererValues = within(multiLineRenderer).getAllByTestId('reltio-attribute-value');
        expect(multiLineRendererLabel).toHaveTextContent('Role');
        expect(multiLineRendererValues).toHaveLength(2);
        expect(multiLineRendererValues[0]).toHaveTextContent('Some role 1');
        expect(multiLineRendererValues[1]).toHaveTextContent('Some role 2');
        var oneLineRenderer = screen.getByTestId('one-line-renderer');
        var oneLineRendererLabel = within(oneLineRenderer).getByTestId('reltio-attribute-label');
        var oneLineRendererValues = within(oneLineRenderer).getAllByTestId('reltio-attribute-value');
        expect(oneLineRendererLabel).toHaveTextContent('Name');
        expect(oneLineRendererValues).toHaveLength(1);
        expect(oneLineRendererValues[0]).toHaveTextContent('Some name');
    });
    it('should render pagers for attributes and analytics attributes with same name', function () {
        var attrTypes = [
            {
                label: 'Name',
                name: 'Name',
                type: 'String',
                hidden: false,
                uri: 'configuration/entityTypes/HCP/attributes/Name',
                access: ['CREATE', 'READ', 'INITIATE_CHANGE_REQUEST', 'UPDATE']
            },
            {
                label: 'Analytics Name',
                name: 'Name',
                type: 'String',
                hidden: false,
                uri: 'configuration/entityTypes/HCP/analyticsAttributes/Name',
                access: ['CREATE', 'READ', 'INITIATE_CHANGE_REQUEST', 'UPDATE']
            }
        ];
        var entity = __assign(__assign({}, defaultEntity), { attributes: {
                Name: [
                    {
                        ov: true,
                        type: 'configuration/entityTypes/HCP/attributes/Name',
                        uri: 'entities/xT3HJZ7/attributes/Name/8Ans02G',
                        value: 'Simple Name value'
                    }
                ]
            }, analyticsAttributes: {
                Name: [
                    {
                        ov: true,
                        type: 'configuration/entityTypes/HCP/analyticsAttributes/Name',
                        uri: 'entities/xT3HJZ7/analyticsAttributes/Name/Bh20IkX',
                        value: 'Analytics Name value'
                    }
                ]
            } });
        var props = __assign(__assign({}, defaultProps), { attrTypes: attrTypes, entity: entity });
        setUp({ props: props });
        var oneLineRenderers = screen.getAllByTestId('one-line-renderer');
        within(oneLineRenderers[0]).getByText('Name');
        within(oneLineRenderers[0]).getByText('Simple Name value');
        within(oneLineRenderers[1]).getByText('Analytics Name');
        within(oneLineRenderers[1]).getByText('Analytics Name value');
    });
    it('should not render pagers for attribute type without READ permission', function () {
        var attrTypes = __spreadArray([], attributesList, true);
        attrTypes[0] = __assign({}, attrTypes[0]);
        attrTypes[0].access = ['DELETE', 'UPDATE'];
        var props = __assign(__assign({}, defaultProps), { attrTypes: attrTypes });
        setUp({ props: props });
        screen.getByText('Name');
        expect(screen.queryByText('Role')).not.toBeInTheDocument();
    });
    it('should not render pagers for attributes without ov values', function () {
        var entity = __assign(__assign({}, defaultEntity), { attributes: __assign(__assign({}, defaultEntity.attributes), { Role: [
                    {
                        //missing 'ov' should be interpreted as ov value
                        type: 'configuration/entityTypes/HCP/attributes/Role',
                        uri: 'entities/xT3HJZ7/attributes/Role/nAs391Ab',
                        value: 'Some role 1'
                    },
                    {
                        ov: false,
                        type: 'configuration/entityTypes/HCP/attributes/Role',
                        uri: 'entities/xT3HJZ7/attributes/Role/8AmySE1',
                        value: 'Some role 2'
                    }
                ], Name: [
                    {
                        ov: false,
                        type: 'configuration/entityTypes/HCP/attributes/Name',
                        uri: 'entities/xT3HJZ7/attributes/Name/8Ans02G',
                        value: 'Some name'
                    }
                ] }) });
        var props = __assign(__assign({}, defaultProps), { entity: entity });
        setUp({ props: props });
        expect(screen.getByTestId('reltio-attribute-label')).toHaveTextContent('Role');
        expect(screen.getByTestId('reltio-attribute-value')).toHaveTextContent('Some role 1');
    });
    it('should render pagers for attributes with ov false if showNonOv is true', function () {
        var entity = __assign(__assign({}, defaultEntity), { attributes: __assign(__assign({}, defaultEntity.attributes), { Name: [
                    {
                        ov: false,
                        type: 'configuration/entityTypes/HCP/attributes/Name',
                        uri: 'entities/xT3HJZ7/attributes/Name/8Ans02G',
                        value: 'Some name'
                    }
                ] }) });
        var props = __assign(__assign({}, defaultProps), { entity: entity, showNonOv: true });
        setUp({ props: props });
        screen.getByText('Name');
        screen.getByText('Some name');
    });
    it('should render BranchDecorator for children', function () {
        var props = __assign(__assign({}, defaultProps), { drawLines: true, children: React.createElement("span", null, "Children") });
        setUp({ props: props });
        var decorator = screen.getAllByTestId('branch-decorator')[0];
        expect(decorator).toHaveTextContent('Children');
    });
    it('should render show more and show less', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user, labels;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { max: 1 });
                    user = setUp({ props: props }).user;
                    labels = screen.getAllByTestId('reltio-attribute-label');
                    expect(labels).toHaveLength(1);
                    expect(labels[0]).toHaveTextContent('Role');
                    return [4 /*yield*/, user.click(screen.getByText('Show more'))];
                case 1:
                    _a.sent();
                    labels = screen.getAllByTestId('reltio-attribute-label');
                    expect(labels).toHaveLength(2);
                    expect(labels[0]).toHaveTextContent('Role');
                    expect(labels[1]).toHaveTextContent('Name');
                    return [4 /*yield*/, user.click(screen.getByText('Show less'))];
                case 2:
                    _a.sent();
                    labels = screen.getAllByTestId('reltio-attribute-label');
                    expect(labels).toHaveLength(1);
                    expect(labels[0]).toHaveTextContent('Role');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should not render show more and show less', function () {
        setUp();
        expect(screen.queryByText('Show more')).not.toBeInTheDocument();
        expect(screen.queryByText('Show less')).not.toBeInTheDocument();
    });
    it('should not render show more and show less when total count of items is lower than max', function () {
        var props = __assign(__assign({}, defaultProps), { max: 4 });
        setUp({ props: props });
        expect(screen.queryByText('Show more')).not.toBeInTheDocument();
        expect(screen.queryByText('Show less')).not.toBeInTheDocument();
    });
    describe('fixed type uris behavior', function () {
        var attrTypes = __spreadArray(__spreadArray([], attributesList, true), [EntityAttrTypes.roles, EntityAttrTypes.tags], false);
        var metadata = {
            entityTypes: [__assign(__assign({}, defaultMetadata.entityTypes[0]), { attributes: attrTypes })],
            roles: [{ uri: 'configuration/roles/Pharmacy', label: 'Pharmacy', description: '' }]
        };
        var entity = __assign(__assign({}, defaultEntity), { roles: ['configuration/roles/Pharmacy'], tags: ['tag1', 'tag2'] });
        it('should render show more/show less buttons and always show roles/tags if they have values', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, mdmValues, user, labels;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { attrTypes: attrTypes, entity: entity, max: 1 });
                        mdmValues = { entity: entity, metadata: metadata };
                        user = setUp({ props: props, mdmValues: mdmValues }).user;
                        labels = screen.getAllByTestId('reltio-attribute-label');
                        expect(labels).toHaveLength(3);
                        expect(labels[0]).toHaveTextContent('Role');
                        expect(labels[1]).toHaveTextContent('Roles');
                        expect(labels[2]).toHaveTextContent('Tags');
                        return [4 /*yield*/, user.click(screen.getByText('Show more'))];
                    case 1:
                        _a.sent();
                        labels = screen.getAllByTestId('reltio-attribute-label');
                        expect(labels).toHaveLength(4);
                        expect(labels[0]).toHaveTextContent('Role');
                        expect(labels[1]).toHaveTextContent('Name');
                        expect(labels[2]).toHaveTextContent('Roles');
                        expect(labels[3]).toHaveTextContent('Tags');
                        return [4 /*yield*/, user.click(screen.getByText('Show less'))];
                    case 2:
                        _a.sent();
                        labels = screen.getAllByTestId('reltio-attribute-label');
                        expect(labels).toHaveLength(3);
                        expect(labels[0]).toHaveTextContent('Role');
                        expect(labels[1]).toHaveTextContent('Roles');
                        expect(labels[2]).toHaveTextContent('Tags');
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not render show more and show less', function () {
            var props = __assign(__assign({}, defaultProps), { attrTypes: attrTypes, entity: entity });
            var mdmValues = { entity: entity, metadata: metadata };
            setUp({ props: props, mdmValues: mdmValues });
            expect(screen.queryByText('Show more')).not.toBeInTheDocument();
            expect(screen.queryByText('Show less')).not.toBeInTheDocument();
        });
        it('should not render show more and show less when total count of items is lower than max', function () {
            var props = __assign(__assign({}, defaultProps), { attrTypes: attrTypes, entity: entity, max: 4 });
            var mdmValues = { entity: entity, metadata: metadata };
            setUp({ props: props, mdmValues: mdmValues });
            expect(screen.queryByText('Show more')).not.toBeInTheDocument();
            expect(screen.queryByText('Show less')).not.toBeInTheDocument();
        });
    });
});
