import Decimal from 'decimal.js'

export const stringArrayConverter = {
    consume: (value) => {
        if (!value) return null
        if (typeof value === 'string') return value.split(',')
        return value
    },
    prepare: (value) => {
        if (value) return value.join(',')
        return null
    }
}

export const jsonConverter = {
    consume: (value) => {
        if (value === null || value === undefined) return null
        try {
            return JSON.parse(value)
        } catch (error) {}
        return null
    },
    prepare: (value) => {
        if (value === null || value === undefined) return null
        return JSON.stringify(value)
    }
}

export const decimalConverter = {
    consume: (value) => {
        if (value === null || value === undefined) return null
        try {
            return new Decimal(value)
        } catch (e) {}
        return null
    },
    prepare: (value) => {
        if (value instanceof Decimal) return value.toString()
        return value
    }
}
