import { $objectProperties } from '~/services/utils/objectProperties'
import { AnyJson } from '~/types/interfaces'

describe('cloneAndFlatten()', () => {
  it('should throw an error if args is not an object', () =>
    expect(() => $objectProperties.cloneAndFlatten(String as AnyJson)).toThrowError('Expecting an object parameter')
  )

  it('should return same values for flat objects', () =>
    expect($objectProperties.cloneAndFlatten({ foo: 1, bar: 'baz' })).toStrictEqual({ foo: 1, bar: 'baz' })
  )
  it('should copy null values', () =>
    expect($objectProperties.cloneAndFlatten({ foo: null })).toStrictEqual({ foo: null })
  )
  it('should clone the input', () => {
    const object = { foo: 1, bar: 'baz' }
    const flatObject = $objectProperties.cloneAndFlatten(object)
    expect(flatObject).not.toBe(object)
    flatObject.foo = 2
    expect(object.foo).toEqual(1)
  })
  it('should flatten nested objects', () =>
    expect($objectProperties.cloneAndFlatten({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } })).toStrictEqual({ a: 1, 'b.c': 2, 'd.e': 3, 'd.f.g': 4, 'd.f.h': 5 })
  )
  it('should not flatten arrays', () =>
    expect($objectProperties.cloneAndFlatten({ a: [1, 2, 3] })).toStrictEqual({ a: [1, 2, 3] })
  )
  it('should not flatten strings', () =>
    expect($objectProperties.cloneAndFlatten({ a: 'hello, world' })).toStrictEqual({ a: 'hello, world' })
  )
  it('should not flatten dates', () => {
    const d = new Date()
    expect($objectProperties.cloneAndFlatten({ a: d })).toStrictEqual({ a: d })
  })
  it('should not flatten excluded properties', () =>
    expect($objectProperties.cloneAndFlatten({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } }, ['d'])).toStrictEqual({ a: 1, 'b.c': 2, d: { e: 3, f: { g: 4, h: 5 } } })
  )
})

describe('cloneAndNest()', () => {
  it('should throw an error if args is not an object', () =>
    expect(() => $objectProperties.cloneAndNest(String as AnyJson)).toThrowError('Expecting an object parameter')
  )

  it('should return same values for flat objects', () =>
    expect($objectProperties.cloneAndNest({ foo: 1, bar: 'baz' })).toStrictEqual({ foo: 1, bar: 'baz' })
  )
  it('should copy null values', () =>
    expect($objectProperties.cloneAndNest({ foo: null })).toStrictEqual({ foo: null })
  )
  it('should clone the input', () => {
    const object = { foo: 1, bar: 'baz' }
    const nestedObject = $objectProperties.cloneAndNest(object)
    expect(nestedObject).not.toBe(object)
    nestedObject.foo = 2
    expect(object.foo).toEqual(1)
  })
  it('should nest flattened objects', () =>
    expect($objectProperties.cloneAndNest({ a: 1, 'b.c': 2, 'd.e': 3, 'd.f.g': 4, 'd.f.h': 5 })).toStrictEqual({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } })
  )
  it('should not error on null nested objects', () =>
    expect($objectProperties.cloneAndNest({ a: null, 'a.b': null })).toStrictEqual({ a: null })
  )
})

describe('sortObjectByKey()', () => {
  it('should throw an error if args is not an array', () =>
    expect(() => $objectProperties.sortObjectByKey('foo')).toThrowError('Expecting an object parameter')
  )
  it('should sort an array by his keys', () =>
    expect($objectProperties.sortObjectByKey({ b: 1, d: 2, c: 3, a: 4 })).toStrictEqual({ a: 4, b: 1, c: 3, d: 2 })
  )
})
