// ConvertBashScriptRunner ...
func ConvertBashScriptRunner(convertedWorkflowStep stepmanModels.StepModel) ([]bitriseModels.StepListItemModel, error) {
	newStepID := NewScriptStepID

	inputConversionMap := map[string]string{}

	_, found, err := utils.GetInputByKey(convertedWorkflowStep.Inputs, "content")
	if err != nil {
		return []bitriseModels.StepListItemModel{}, err
	}
	if found {
		inputConversionMap = map[string]string{
			"content":     "content",
			"working_dir": "BASH_SCRIPT_WORKING_DIR",
		}
	} else {
		inputConversionMap = map[string]string{
			"content":     "__INPUT_FILE__",
			"working_dir": "BASH_SCRIPT_WORKING_DIR",
		}
	}

	return utils.ConvertStepAndCreateStepListItem(convertedWorkflowStep, newStepID, inputConversionMap)
}
// ConvertXcodeBuilderFlavorBitriseUnittest ...
func ConvertXcodeBuilderFlavorBitriseUnittest(convertedWorkflowStep stepmanModels.StepModel) ([]bitriseModels.StepListItemModel, error) {
	simulatorOsVersion := "latest"
	simulatorDevice := ""

	// Converter function overwrites
	convertStepsInputs := func(originalInputs, diffInputs []envmanModels.EnvironmentItemModel, conversionMap map[string]string) ([]envmanModels.EnvironmentItemModel, error) {
		convertedInputs := []envmanModels.EnvironmentItemModel{}
		for _, originalInput := range originalInputs {
			originalInputKey, originalInputValue, err := originalInput.GetKeyValuePair()
			if err != nil {
				return []envmanModels.EnvironmentItemModel{}, err
			}

			originalInputOptions, err := originalInput.GetOptions()
			if err != nil {
				return []envmanModels.EnvironmentItemModel{}, err
			}

			conversionInputKey, found := conversionMap[originalInputKey]
			if found == false {
				convertedInputs = append(convertedInputs, originalInput)
				continue
			}

			diffInput, found, err := utils.GetInputByKey(diffInputs, conversionInputKey)
			if err != nil {
				return []envmanModels.EnvironmentItemModel{}, err
			}
			if !found {
				convertedInputs = append(convertedInputs, originalInput)
				continue
			}

			_, diffInputValue, err := diffInput.GetKeyValuePair()
			if err != nil {
				return []envmanModels.EnvironmentItemModel{}, err
			}
			if diffInputValue == "" {
				diffInputValue = originalInputValue
			}

			diffInputOptions, err := diffInput.GetOptions()
			if err != nil {
				return []envmanModels.EnvironmentItemModel{}, err
			}

			if diffInputOptions.IsExpand != nil {
				originalInputOptions.IsExpand = pointers.NewBoolPtr(*diffInputOptions.IsExpand)
			}

			// Get ios version from old comma separated form
			if originalInputKey == "simulator_device" {
				simulatorDevice = diffInputValue
				if diffInputValue != "" {
					versionSplit := strings.Split(diffInputValue, ",")
					if len(versionSplit) == 2 {
						simulatorDevice = versionSplit[0]
						simulatorOsVersion = versionSplit[1]
						if strings.HasPrefix(simulatorOsVersion, "OS=") {
							simulatorOsVersion = strings.Replace(simulatorOsVersion, "OS=", "", -1)
						}
					}
				}
			}

			convertedInput := envmanModels.EnvironmentItemModel{
				originalInputKey:        diffInputValue,
				envmanModels.OptionsKey: originalInputOptions,
			}

			convertedInputs = append(convertedInputs, convertedInput)
		}

		for idx, input := range convertedInputs {
			specKey, _, err := input.GetKeyValuePair()
			if err != nil {
				return []envmanModels.EnvironmentItemModel{}, err
			}
			if specKey == "simulator_device" {
				input[specKey] = simulatorDevice
				convertedInputs[idx] = input
			}
			if specKey == "simulator_os_version" {
				input[specKey] = simulatorOsVersion
				convertedInputs[idx] = input
			}
		}

		return convertedInputs, nil
	}

	convertStep := func(convertedWorkflowStep stepmanModels.StepModel, newStepID string, inputConversionMap map[string]string) (stepmanModels.StepModel, string, error) {
		// The new StepLib version of step
		specStep, version, err := utils.GetStepFromNewSteplib(newStepID, utils.BitriseVerifiedStepLibGitURI)
		if err != nil {
			return stepmanModels.StepModel{}, "", err
		}
		if convertedWorkflowStep.Title != nil && *convertedWorkflowStep.Title != "" {
			specStep.Title = pointers.NewStringPtr(*convertedWorkflowStep.Title)
		}
		if convertedWorkflowStep.IsAlwaysRun != nil {
			specStep.IsAlwaysRun = pointers.NewBoolPtr(*convertedWorkflowStep.IsAlwaysRun)
		}

		// Merge new StepLib version inputs, with old workflow defined
		mergedInputs, err := convertStepsInputs(specStep.Inputs, convertedWorkflowStep.Inputs, inputConversionMap)
		if err != nil {
			return stepmanModels.StepModel{}, "", err
		}
		specStep.Inputs = mergedInputs

		return specStep, version, nil
	}

	stepListItems, err := utils.CertificateStep()
	if err != nil {
		return []bitriseModels.StepListItemModel{}, err
	}

	// Convert Xcode test step
	newStepID := NewXcodeTest
	inputConversionMap := map[string]string{
		"workdir":          "XCODE_BUILDER_PROJECT_ROOT_DIR_PATH",
		"project_path":     "XCODE_BUILDER_PROJECT_PATH",
		"scheme":           "XCODE_BUILDER_SCHEME",
		"simulator_device": "XCODE_BUILDER_UNITTEST_PLATFORM_NAME",
	}

	newStep, version, err := convertStep(convertedWorkflowStep, newStepID, inputConversionMap)
	if err != nil {
		return []bitriseModels.StepListItemModel{}, err
	}

	stepIDDataString := newStepID + "@" + version
	stepListItems = append(stepListItems, bitriseModels.StepListItemModel{stepIDDataString: newStep})

	return stepListItems, nil
}