{"size": 1526, "ext": "js", "max_stars_repo_path": "modules/gee/docker/src/jobs/ee/image/preview.js", "max_stars_repo_name": "BuddyVolly/sepal", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "modules/gee/docker/src/jobs/ee/image/preview.js", "max_issues_repo_name": "BuddyVolly/sepal", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "modules/gee/docker/src/jobs/ee/image/preview.js", "max_forks_repo_name": "BuddyVolly/sepal", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.1428571429, "max_line_length": 98, "alphanum_fraction": 0.5557011796}
const {job} = require('root/jobs/job')

const worker$ = ({recipe, bands}) => {
    const ImageFactory = require('sepal/ee/imageFactory')
    const ee = require('ee')
    const {switchMap} = require('rx/operators')

    const {getImage$, getVisParams$} = ImageFactory(recipe, bands)
    return getImage$().pipe(
        switchMap(image => getVisParams$(image).pipe(
            switchMap(visParams =>
                visParams.hsv
                    ? ee.getMap$(hsvToRgb(image, visParams))
                    : ee.getMap$(image, visParams))
            )
        )
    )
}

const hsvToRgb = (image, visParams) => {
    const stretchImage = (image, fromRange, toRange) => {
        return image.expression('toMin + (i - fromMin) * (toMax - toMin) / (fromMax - fromMin)', {
            'fromMin': fromRange[0],
            'fromMax': fromRange[1],
            'toMin': toRange[0],
            'toMax': toRange[1],
            'i': image
        })
    }
    const stretch = visParams.stretch || []
    const bands = visParams.bands
    stretch.forEach((toRange, i) => {
        const fromRange = [visParams['min'][i], visParams['max'][i]]
        if (!toRange)
            toRange = [0, 1]
        const band = bands[i]
        const stretched = stretchImage(image.select(band), fromRange, toRange)
            .rename(band)
        image = image.addBands(stretched, null, true)
    })
    return image.select(bands).hsvToRgb().float()
}

module.exports = job({
    jobName: 'Preview',
    jobPath: __filename,
    worker$
})
