import * as path from 'path'
import { sync as read } from "@plastichub/fs/read"


import {
    productLaserTask,
    productCADTask
} from './product'


const getProducts = (branch: string) => {
    const conf = read("./config/machines.json", "json") || {}
    if (branch) {
        conf['all'] = [...conf["extruders"]]
        return conf[branch] || []
    } else {
        return Object.values(conf).flat()
    }
}

export const grunt = (grunt) => {
    grunt.initConfig({
        pkg: grunt.file.readJSON("package.json"),
        sshexec: {
            update: {
                debug: true,
                verbose: true,
                commands: 'sh update-osr.sh'
            }
        }
    })

    
    grunt.loadNpmTasks('grunt-parallel')
    grunt.loadNpmTasks("grunt-extend-config")

    const product_laser_tasks = []
    const product_cad_tasks = []
    

    const productTasks = (items) => {
        items.forEach((i) => {
            productCADTask(grunt, i, {}, product_cad_tasks)
            //productLaserTask(grunt, i, {}, product_laser_tasks)            
        })
    }

    const _products = getProducts(grunt.option('branch') || "current")
    productTasks(_products)
    
    grunt.registerTask('products_update', [
        'products-cad',
        //'products-laser'
    ])

    grunt.registerTask('products_sync', [
        // 'sshexec:update'
    ])

    grunt.registerTask('products_full', [
        'products_update',
        'products_sync'
    ])

    require("@plastichub/osr-tasks").initConfig(grunt, {})
};

module.exports = grunt