import FS        from "fs";
import Path      from "path";
import Process   from "process";
import Assertion from "assert";

import Chalk from "chalk";

import { Distribution } from "./distribution.js";

/***
 * Extension of {@link Distribution}
 */

class Package extends Distribution {
    public static readonly file = "package.json";
    public static readonly folder = "ci";

    private static readonly resolve = () => Path.join( Process.cwd(), Package.folder, Package.file );
    private static readonly source = () => Path.join( Process.cwd(), Package.file );
    private static readonly target = () => Package.source();

    public static readonly contents?: typeof import("../package.json") = JSON.parse( FS.readFileSync( Package.source(), { encoding: "utf-8" } ) );

    /***
     *
     * @param target {string} - Target Directory for Distribution
     * @param debug {boolean} - Debug Parameter
     *
     * @constructs
     *
     */

    constructor( target: string, debug: boolean = false ) {
        super( target, debug );

        Assertion.equal( FS.existsSync( target ), true );
    }

    static async reconfigure() {
        console.debug( "[Debug]", "Establishing" + " " + Chalk.bold.blueBright("package.json") + " " + "File ..." );

        const configuration = JSON.stringify( {
            ... {
                name: [ Package.contents.name, "cdktf" ].join( "-" ),
                private: true,
                version: "0.0.1",
                type: "module",
                main: "main.js",
                types: "main.ts",
                scripts: {
                    build: "tsc",
                    compile: "tsc --watch",
                    get: "cdktf get",
                    synth: "cdktf synth",
                    deploy: "cdktf deploy",
                    start: "npm run initialize",
                    initialize: "node --enable-source-maps -r source-map-support/register --es-module-specifier-resolution node ."
                },
                dependencies: {
                    "@cloud-technology/constructs": Package.Import( Path.join( Package.PWD, "package.json" ) ).version,
                    "typescript": "^4.5.5"
                },
                devDependencies: {
                    "source-map-support": "latest",
                    "@types/node": "latest",
                    "@types/source-map-support": "latest"
                }
            }
        }, null, 4 );

        await Package.write( Package.resolve(), configuration );

        Assertion.equal( FS.existsSync( Package.target() ), true );
    }
}

export { Package };

export default Package;