//
//  AnalyticsConfiguration+Runnable.swift
//  
//
//  Created by Loris Perret on 08/12/2023.
//

import Foundation

extension AnalyticsConfiguration: Runnable {

    func run(projectDirectory: String, force: Bool) {
        let args = getArguments(projectDirectory: projectDirectory, force: force)
        Analytics.main(args)
    }

    func getArguments(projectDirectory: String, force: Bool) -> [String] {
        var args = [String]()

        if force {
            args += ["-f"]
        }

        args += [
            inputFile.prependIfRelativePath(projectDirectory),
            "--target",
            target,
            "--output-file",
            outputFile.prependIfRelativePath(projectDirectory)
        ]

        // Add optional parameters
        [
            ("--visibility", visibility),
            ("--static-members", staticMembers?.description)
        ].forEach { argumentName, argumentValue in
            if let argumentValue {
                args += [
                    argumentName,
                    argumentValue
                ]
            }
        }

        return args
    }
}
