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);
};
import React from 'react';
import i18n from 'ui-i18n';
import { screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { DataTypes, FilterOptions } from '@reltio/mdm-sdk';
import { FilterChip } from './FilterChip';
import { MdmModuleProvider } from '../../../../contexts/MdmModuleContext';
var formatDateValue = function (value) { return i18n.date(value, 'L'); };
var defaultProps = {
    filterValue: '123',
    filterOption: {
        value: FilterOptions.EQUALS,
        label: 'equals'
    },
    dataTypeDefinition: {
        type: DataTypes.TYPE_INT,
        uri: 'configuration/entityTypes/HCP/attributes/Int'
    },
    onDelete: jest.fn()
};
var setUp = function (props, mdmValues) {
    if (props === void 0) { props = defaultProps; }
    if (mdmValues === void 0) { mdmValues = {}; }
    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(FilterChip, __assign({}, props)), { wrapper: Providers })), { user: user });
};
describe('Filter chip tests', function () {
    it('should render chip correctly', function () {
        setUp();
        screen.getByText('equals:');
        screen.getByText('123');
    });
    it('should render only filterOption as label if filter does not accept values', function () {
        var props = __assign(__assign({}, defaultProps), { filterOption: { value: FilterOptions.EXISTS, label: 'exists' } });
        setUp(props);
        screen.getByText('exists');
    });
    it('should render date "on" filterOption', function () {
        var value = 1572480000000;
        var props = __assign(__assign({}, defaultProps), { dataTypeDefinition: { type: DataTypes.TYPE_ACTIVENESS_DATE }, filterValue: value, filterOption: { value: FilterOptions.ON, label: 'on' } });
        setUp(props);
        screen.getByText('on:');
        screen.getByText(formatDateValue(value));
    });
    it('should render date "within" filterOptions with string value', function () {
        var props = __assign(__assign({}, defaultProps), { dataTypeDefinition: { type: DataTypes.TYPE_DATE }, filterValue: 'lastWeek', filterOption: { value: FilterOptions.WITHIN, label: 'within' } });
        setUp(props);
        screen.getByText('within:');
        screen.getByText('Last 7 days');
    });
    it('should render date "within" filterOptions with range value', function () {
        var value = [1590994800000, 1593154800000];
        var props = __assign(__assign({}, defaultProps), { dataTypeDefinition: { type: DataTypes.TYPE_DATE }, filterValue: value, filterOption: { value: FilterOptions.WITHIN, label: 'within' } });
        setUp(props);
        var range = value.map(formatDateValue);
        screen.getByText('within:');
        screen.getByText("".concat(range[0], " - ").concat(range[1]));
    });
    it('should take into account date masks', function () {
        var dateMask = '99-99-9999';
        var mdmValues = { dateMask: dateMask };
        var value = [1590994800000, 1593154800000];
        var props = __assign(__assign({}, defaultProps), { filterValue: value, dataTypeDefinition: { type: DataTypes.TYPE_DATE }, filterOption: { value: FilterOptions.WITHIN, label: 'within' } });
        setUp(props, mdmValues);
        var range = value.map(formatDateValue).map(function (str) { return str.split('/').join('-'); });
        screen.getByText('within:');
        screen.getByText("".concat(range[0], " - ").concat(range[1]));
    });
    it('should take into account attributes presentation', function () {
        var mdmValues = {
            attributesPresentation: {
                'configuration/entityTypes/HCP/attributes/Int': {
                    prefix: '<',
                    suffix: '>'
                }
            }
        };
        setUp(defaultProps, mdmValues);
        screen.getByText('equals:');
        screen.getByText('<123>');
    });
});
