"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 moment_1 = __importDefault(require("moment"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var DataTypeValue_1 = require("./DataTypeValue");
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { formatDataTypeValue: jest.fn().mockImplementation(jest.requireActual('@reltio/mdm-sdk').formatDataTypeValue) })); });
var defaultAttributesPresentation = { attr1: {} };
var defaultDateMask = '99-99-99';
var defaultDateTimeMask = '99-99-99 99:99:99';
var defaultMdmValues = {
    attributesPresentation: defaultAttributesPresentation,
    dateMask: defaultDateMask,
    dateTimeMask: defaultDateTimeMask
};
var setUp = function (props, mdmValues) {
    if (mdmValues === void 0) { mdmValues = defaultMdmValues; }
    var Providers = function (_a) {
        var children = _a.children;
        return react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: mdmValues }, children);
    };
    return (0, react_2.render)(react_1.default.createElement(DataTypeValue_1.DataTypeValue, __assign({}, props)), { wrapper: Providers });
};
describe('DataTypeValue tests', function () {
    var checkFormatDataTypeCall = function (_a) {
        var _b = _a.attributesPresentation, attributesPresentation = _b === void 0 ? defaultAttributesPresentation : _b, _c = _a.dateMask, dateMask = _c === void 0 ? defaultDateMask : _c, _d = _a.dateTimeMask, dateTimeMask = _d === void 0 ? defaultDateTimeMask : _d, dataTypeDefinition = _a.dataTypeDefinition, _e = _a.disableTimestampLocalization, disableTimestampLocalization = _e === void 0 ? false : _e, value = _a.value;
        expect(mdm_sdk_1.formatDataTypeValue).toHaveBeenCalledWith({ attributesPresentation: attributesPresentation, dateMask: dateMask, dateTimeMask: dateTimeMask, dataTypeDefinition: dataTypeDefinition, disableTimestampLocalization: disableTimestampLocalization }, value);
    };
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should render regular attribute', function () {
        var value = '123';
        var dataTypeDefinition = { type: 'String' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.queryByRole('link')).not.toBeInTheDocument();
        react_2.screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render attribute with timestamp and system format if masks were not configured', function () {
        var value = '03/05/2020';
        var dataTypeDefinition = { type: 'Date' };
        var mdmValues = __assign(__assign({}, defaultMdmValues), { dateMask: undefined, dateTimeMask: undefined });
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition }, mdmValues);
        expect(react_2.screen.queryByRole('link')).not.toBeInTheDocument();
        react_2.screen.getByText(value);
        checkFormatDataTypeCall({
            dataTypeDefinition: dataTypeDefinition,
            value: value,
            dateMask: moment_1.default.localeData().longDateFormat('L'),
            dateTimeMask: moment_1.default.localeData().longDateFormat('L') + ' ' + moment_1.default.localeData().longDateFormat('LTS')
        });
    });
    it('should render attribute with date and dateMask', function () {
        var value = '03/05/2020';
        var dataTypeDefinition = { type: 'Date' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.queryByRole('link')).not.toBeInTheDocument();
        react_2.screen.getByText('03-05-20');
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render URL attribute as Link', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.getByText(value)).toHaveAttribute('href', value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render Blog URL attribute as Link', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'Blog URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.getByText(value)).toHaveAttribute('href', value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render Image URL attribute as Link', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'Image URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.getByText(value)).toHaveAttribute('href', value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should make absolute for url attribute', function () {
        var value = 'www.reltio.com';
        var dataTypeDefinition = { type: 'URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        var expectedUrl = 'http://' + value; // NOSONAR
        expect(react_2.screen.getByText(value)).toHaveAttribute('href', expectedUrl);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render Email attribute as Link', function () {
        var value = 'it@reltio.com';
        var dataTypeDefinition = { type: 'Email' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.getByText(value)).toHaveAttribute('href', 'mailto:' + value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not render attribute as Link if its type is not URL/BLOG_URL/IMAGE_URL/Email', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'notURL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.queryByRole('link')).not.toBeInTheDocument();
        react_2.screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not render url attribute as Link if "rich" prop is false', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition, rich: false });
        expect(react_2.screen.queryByRole('link')).not.toBeInTheDocument();
        react_2.screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not render url attribute as Link if url is not valid', function () {
        var value = 'httpsreltiocom';
        var dataTypeDefinition = { type: 'URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.queryByRole('link')).not.toBeInTheDocument();
        react_2.screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not render email attribute as Link if email is not valid', function () {
        var value = 'itreltio.com';
        var dataTypeDefinition = { type: 'Email' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(react_2.screen.queryByRole('link')).not.toBeInTheDocument();
        react_2.screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not crash and keep localization on when dataTypeDefinition has no uri and flag is enabled', function () {
        var value = '2025-07-09T23:10:09.000+0000';
        var dataTypeDefinition = { type: 'Timestamp' };
        var mdmValues = __assign(__assign({}, defaultMdmValues), { disableTimestampLocalization: true });
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition }, mdmValues);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value, disableTimestampLocalization: false });
    });
    it('should disable localization for regular Timestamp attribute when flag is enabled', function () {
        var value = '2025-07-09T23:10:09.000+0000';
        var dataTypeDefinition = {
            type: 'Timestamp',
            uri: 'configuration/entityTypes/Individual/attributes/HireDate'
        };
        var mdmValues = __assign(__assign({}, defaultMdmValues), { disableTimestampLocalization: true });
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition }, mdmValues);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value, disableTimestampLocalization: true });
    });
    it('should keep localization on for system Timestamp attribute even when flag is enabled', function () {
        var value = '2025-07-09T23:10:09.000+0000';
        var dataTypeDefinition = { type: 'Timestamp', uri: 'createdTime' };
        var mdmValues = __assign(__assign({}, defaultMdmValues), { disableTimestampLocalization: true });
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition }, mdmValues);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value, disableTimestampLocalization: false });
    });
});
