//
//  ImagesConfiguration+Runnable.swift
//
//
//  Created by Thibaut Schmitt on 30/08/2022.
//

import Foundation

extension ImagesConfiguration: Runnable {

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

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

        if force {
            args += ["-f"] // Images has a -f and -F options
        }

        args += [
            inputFile.prependIfRelativePath(projectDirectory),
            "--xcassets-path",
            xcassetsPath.prependIfRelativePath(projectDirectory)
        ]

        // Add optional parameters
        [
            ("--extension-output-path", extensionOutputPath?.prependIfRelativePath(projectDirectory)),
            ("--extension-name", extensionName),
            ("--extension-name-ui-kit", extensionNameUIKit),
            ("--extension-suffix", extensionSuffix),
            ("--visibility", visibility),
            ("--asset-bundle", assetBundle),
            ("--static-members", staticMembers?.description)
        ].forEach { argumentName, argumentValue in
            if let argumentValue {
                args += [
                    argumentName,
                    argumentValue
                ]
            }
        }

        return args
    }
}
