import fs from 'node:fs';
import { platform } from 'node:os';
import {lookpathSync} from 'find-bin';

export let kicad_path: string | undefined;
export let kicad_cli_path: string | undefined;

export class KiCAD {
    #possible_paths: string[] = [
        'C:/Program Files/KiCad/9.0/',
        'C:/Program Files/KiCad/7.0/',                                  // windows
        'C:/Program Files/KiCad/8.0/',
        '/usr/share/kicad/',                                            // linux
        '/Applications/KiCad/KiCad.app/Contents/MacOS'          // mac?
    ]

    constructor() {
        // push typecad.json#kicad_path into the list to check

        for (const path of this.#possible_paths) {
            if (fs.existsSync(path)) {
            kicad_path = path; // share/kicad/symbols
            break;
            }
        }

        // if it found a path, figure out where 'kicad-cli' is
        if (kicad_path) {
            if (platform() == 'win32') {
                kicad_cli_path = kicad_path + 'bin/kicad-cli.exe';
            }
        }
        
        if (platform() == 'linux') {
            // whereis
            kicad_cli_path = lookpathSync('kicad-cli');
        } else if (platform() == 'darwin') {
            kicad_cli_path = '/opt/homebrew/bin/kicad-cli';
        }
    }
}
