"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 __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 react_2 = require("@testing-library/react");
var useAutopopulationContextValue_1 = require("./useAutopopulationContextValue");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var mdmActions = {
    dependentLookupsEditorTouched: jest.fn()
};
var defaultMdmValues = {
    dependentLookups: {
        editors: {},
        structure: {},
        initedTypeUris: []
    },
    lookupAutocomplete: {
        excludeAttributes: ['someTypeUris']
    }
};
var defaultProps = {
    valueUri: 'uri1',
    attributeTypeUri: 'typeUri1',
    enabled: true
};
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 Providers = function (_a) {
        var children = _a.children;
        return (react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: mdmValues, actions: mdmActions }, children));
    };
    return (0, react_2.renderHook)(useAutopopulationContextValue_1.useAutopopulationContextValue, { initialProps: props, wrapper: Providers });
};
describe('useAutopopulationContextValue behaviour', function () {
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should return null if enabled prop is false', function () {
        var props = __assign(__assign({}, defaultProps), { enabled: false });
        var result = setUp({ props: props }).result;
        expect(result.current.autopopulationContextValue).toBe(null);
    });
    it('should return valuable autopopulationContextValue if enabled prop is true', function () {
        var result = setUp().result;
        expect(result.current.autopopulationContextValue).toEqual({
            id: defaultProps.valueUri,
            isTouched: false,
            onTouch: expect.any(Function)
        });
    });
    it('should set autopopulationContextValue.isTouched as true if editor is touched', function () {
        var dependentLookups = __assign(__assign({}, defaultMdmValues.dependentLookups), { editors: { uri1: { isTouched: true } } });
        var mdmValues = __assign(__assign({}, defaultMdmValues), { dependentLookups: dependentLookups });
        var result = setUp({ mdmValues: mdmValues }).result;
        expect(result.current.autopopulationContextValue.isTouched).toBe(true);
    });
    it('should dispatch dependentLookupsEditorTouched action on onTouch calling', function () {
        var dependentLookups = __assign(__assign({}, defaultMdmValues.dependentLookups), { editors: { uri1: { isTouched: true } } });
        var mdmValues = __assign(__assign({}, defaultMdmValues), { dependentLookups: dependentLookups });
        var result = setUp({ mdmValues: mdmValues }).result;
        result.current.autopopulationContextValue.onTouch(defaultProps.valueUri);
        expect(mdmActions.dependentLookupsEditorTouched).toHaveBeenCalledWith({
            uri: defaultProps.valueUri,
            isTouched: true
        });
    });
    describe('"autopopulationId" behavior', function () {
        var attributeTypeUri = 'configuration/entityTypes/ForDL/attributes/UseCase1/attributes/OrganizationSubType';
        var valueUri = 'entities/1K8HbpTj/attributes/UseCase1/3C3di6JZn/OrganizationSubType/3C3di6WMZ';
        var props = { enabled: true, valueUri: valueUri, attributeTypeUri: attributeTypeUri };
        it('should set "autopopulationId" as valueUri if attributeTypeUri is inside "includeAttributes" config part', function () {
            var lookupAutocomplete = {
                includeAttributes: [attributeTypeUri]
            };
            var mdmValues = __assign(__assign({}, defaultMdmValues), { lookupAutocomplete: lookupAutocomplete });
            var result = setUp({ mdmValues: mdmValues, props: props }).result;
            expect(result.current.autopopulationContextValue.id).toBe(valueUri);
        });
        it('should set "autopopulationId" as valueUri if attributeTypeUri is not inside "excludeAttributes" config part', function () {
            var lookupAutocomplete = {
                excludeAttributes: []
            };
            var mdmValues = __assign(__assign({}, defaultMdmValues), { lookupAutocomplete: lookupAutocomplete });
            var result = setUp({ mdmValues: mdmValues, props: props }).result;
            expect(result.current.autopopulationContextValue.id).toBe(valueUri);
        });
        it('should return null if attributeTypeUri is inside "excludeAttributes" config part', function () {
            var lookupAutocomplete = {
                excludeAttributes: [attributeTypeUri]
            };
            var mdmValues = __assign(__assign({}, defaultMdmValues), { lookupAutocomplete: lookupAutocomplete });
            var result = setUp({ mdmValues: mdmValues, props: props }).result;
            expect(result.current.autopopulationContextValue).toBe(null);
        });
        it('"includeAttributes" should have higher priority', function () {
            var lookupAutocomplete = {
                includeAttributes: [attributeTypeUri],
                excludeAttributes: [attributeTypeUri]
            };
            var mdmValues = __assign(__assign({}, defaultMdmValues), { lookupAutocomplete: lookupAutocomplete });
            var result = setUp({ mdmValues: mdmValues, props: props }).result;
            expect(result.current.autopopulationContextValue.id).toBe(props.valueUri);
        });
    });
});
