import { DataTypes } from '@reltio/mdm-sdk';
import { attributeValueToEditorValue, editorValueToAttributeValue } from './attributesView';
describe('attributesView tests', function () {
    describe('attributeValueToEditorValue', function () {
        it('should return true if attribute is boolean and value is string', function () {
            var attributeValue = {
                value: 'true',
                uri: 'configuration/entityTypes/HCP/attributes/Active'
            };
            var attributeType = {
                type: DataTypes.TYPE_BOOLEAN,
                uri: 'configuration/entityTypes/HCP/attributes/Active',
                name: 'Active'
            };
            expect(attributeValueToEditorValue(attributeValue, attributeType)).toBe(true);
        });
        it('should return false if attribute is boolean and value is string', function () {
            var attributeValue = {
                value: 'false',
                uri: 'configuration/entityTypes/HCP/attributes/Active'
            };
            var attributeType = {
                type: DataTypes.TYPE_BOOLEAN,
                uri: 'configuration/entityTypes/HCP/attributes/Active',
                name: 'Active'
            };
            expect(attributeValueToEditorValue(attributeValue, attributeType)).toBe(false);
        });
        it('should return the same true if attribute is boolean', function () {
            var attributeValue = {
                value: true,
                uri: 'configuration/entityTypes/HCP/attributes/Active'
            };
            var attributeType = {
                type: DataTypes.TYPE_BOOLEAN,
                uri: 'configuration/entityTypes/HCP/attributes/Active',
                name: 'Active'
            };
            expect(attributeValueToEditorValue(attributeValue, attributeType)).toBe(true);
        });
        it('should return value of boolean if attribute is string', function () {
            var attributeValue = {
                value: 'false',
                uri: 'configuration/entityTypes/HCP/attributes/Active'
            };
            var attributeType = {
                type: DataTypes.TYPE_STRING,
                uri: 'configuration/entityTypes/HCP/attributes/Active',
                name: 'Active'
            };
            expect(attributeValueToEditorValue(attributeValue, attributeType)).toBe('false');
        });
        it('should return date plus local TZ offset if attribute is date and value is string', function () {
            var attributeValue = {
                value: '2018-05-12',
                uri: 'configuration/entityTypes/HCP/attributes/Active'
            };
            var attributeType = {
                type: DataTypes.TYPE_DATE,
                uri: 'configuration/entityTypes/HCP/attributes/Active',
                name: 'Active'
            };
            var date = new Date(attributeValue.value);
            var expectedTimestamp = date.getTime() + date.getTimezoneOffset() * 60 * 1000;
            expect(attributeValueToEditorValue(attributeValue, attributeType)).toEqual(new Date(expectedTimestamp));
        });
        it('should return null if attribute is date and value is undefined', function () {
            var attributeValue = { uri: 'configuration/entityTypes/HCP/attributes/Active', value: undefined };
            var attributeType = {
                type: DataTypes.TYPE_DATE,
                uri: 'configuration/entityTypes/HCP/attributes/Active',
                name: 'Active'
            };
            expect(attributeValueToEditorValue(attributeValue, attributeType)).toBe(null);
        });
        describe('TYPE_TIMESTAMP', function () {
            var attributeType = {
                type: DataTypes.TYPE_TIMESTAMP,
                uri: 'configuration/entityTypes/HCP/attributes/Updated',
                name: 'Updated'
            };
            it('returns Date constructed from value when flag is off', function () {
                var ms = Date.UTC(2020, 1, 20, 7, 0, 0);
                var result = attributeValueToEditorValue({ value: ms, uri: '' }, attributeType);
                expect(result).toEqual(new Date(ms));
            });
            it('uses toLocalDate (UTC parts as local) when flag is on (numeric input)', function () {
                var ms = Date.UTC(2020, 1, 20, 7, 15, 30);
                var result = attributeValueToEditorValue({ value: ms, uri: '' }, attributeType, {
                    disableTimestampLocalization: true
                });
                expect(result.getFullYear()).toBe(2020);
                expect(result.getMonth()).toBe(1);
                expect(result.getDate()).toBe(20);
                expect(result.getHours()).toBe(7);
                expect(result.getMinutes()).toBe(15);
                expect(result.getSeconds()).toBe(30);
            });
            it('uses toLocalDate when flag is on (ISO string input)', function () {
                var value = '2020-02-20T07:15:30.000+0000';
                var result = attributeValueToEditorValue({ value: value, uri: '' }, attributeType, {
                    disableTimestampLocalization: true
                });
                expect(result.getFullYear()).toBe(2020);
                expect(result.getMonth()).toBe(1);
                expect(result.getDate()).toBe(20);
                expect(result.getHours()).toBe(7);
                expect(result.getMinutes()).toBe(15);
                expect(result.getSeconds()).toBe(30);
            });
            it('returns null when value is missing', function () {
                expect(attributeValueToEditorValue({ value: undefined, uri: '' }, attributeType)).toBeNull();
                expect(attributeValueToEditorValue({ value: undefined, uri: '' }, attributeType, {
                    disableTimestampLocalization: true
                })).toBeNull();
            });
        });
        it('should return lookup value+lookupCode if attribute is lookup', function () {
            var attributeValue = {
                value: 'KT',
                lookupCode: 'Units Of Measures',
                uri: 'configuration/entityTypes/HCP/attributes/Active'
            };
            var attributeType = {
                type: DataTypes.TYPE_LOOKUP,
                uri: 'configuration/entityTypes/HCP/attributes/Active',
                name: 'Active'
            };
            expect(attributeValueToEditorValue(attributeValue, attributeType)).toEqual({
                lookupCode: 'Units Of Measures',
                value: 'KT'
            });
        });
    });
    describe('editorValueToAttributeValue TYPE_TIMESTAMP', function () {
        var attributeType = {
            type: DataTypes.TYPE_TIMESTAMP,
            uri: 'configuration/entityTypes/HCP/attributes/Updated',
            name: 'Updated'
        };
        it('returns ISO UTC string from local Date when flag is off', function () {
            var localDate = new Date(2020, 1, 20, 7, 0, 0);
            var result = editorValueToAttributeValue(attributeType, localDate);
            expect(typeof result).toBe('string');
            expect(result).toMatch(/Z$|\+00:?00$/);
            expect(new Date(result).getTime()).toBe(localDate.getTime());
        });
        it('preserves local parts as UTC when flag is on', function () {
            var localDate = new Date(2020, 1, 20, 7, 0, 0);
            var result = editorValueToAttributeValue(attributeType, localDate, {
                disableTimestampLocalization: true
            });
            var expectedMs = Date.UTC(localDate.getFullYear(), localDate.getMonth(), localDate.getDate(), localDate.getHours(), localDate.getMinutes(), localDate.getSeconds(), localDate.getMilliseconds());
            expect(new Date(result).getTime()).toBe(expectedMs);
        });
        it('returns empty string when editor value is empty', function () {
            expect(editorValueToAttributeValue(attributeType, null)).toBe('');
            expect(editorValueToAttributeValue(attributeType, null, { disableTimestampLocalization: true })).toBe('');
        });
    });
});
