/*
 * Copyright 2021-2022 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.spotless.glue.ktlint;

import java.io.File;
import java.util.Map;

import org.jetbrains.annotations.NotNull;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot31Dot0Adapter;
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot32Dot0Adapter;
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot34Dot2Adapter;
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot45Dot2Adapter;
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot46Dot0Adapter;
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot47Dot0Adapter;
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompatAdapter;

public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {

	private final Map<String, String> userData;
	private final boolean isScript;
	@NotNull
	private final KtLintCompatAdapter adapter;
	private final boolean useExperimental;
	private final Map<String, Object> editorConfigOverrideMap;

	public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, Map<String, String> userData,
			Map<String, Object> editorConfigOverrideMap) {
		int minorVersion = Integer.parseInt(version.split("\\.")[1]);
		if (version.equals("0.45.2")) {
			this.adapter = new KtLintCompat0Dot45Dot2Adapter();
		} else if (minorVersion >= 47) {
			this.adapter = new KtLintCompat0Dot47Dot0Adapter();
		} else if (minorVersion >= 46) {
			this.adapter = new KtLintCompat0Dot46Dot0Adapter();
		} else if (minorVersion >= 34) {
			this.adapter = new KtLintCompat0Dot34Dot2Adapter();
		} else if (minorVersion >= 32) {
			this.adapter = new KtLintCompat0Dot32Dot0Adapter();
		} else {
			this.adapter = new KtLintCompat0Dot31Dot0Adapter();
		}
		this.useExperimental = useExperimental;
		this.editorConfigOverrideMap = editorConfigOverrideMap;
		this.userData = userData;
		this.isScript = isScript;
	}

	@Override
	public String applyWithFile(String unix, File file) throws Exception {
		return adapter.format(unix, file.getName(), isScript, useExperimental, userData, editorConfigOverrideMap);
	}
}

@@ -1854,35 +1854,66 @@
 if (
-version.equals(%220.45.2%22)) %7B
+minorVersion %3E= 47) %7B%0A%09%09%09// rename RuleSet to RuleProvider
 %0A%09%09%09
@@ -1944,29 +1944,29 @@
 tCompat0Dot4
-5Dot2
+7Dot0
 Adapter();%0A%09
@@ -1994,21 +1994,141 @@
 ion %3E= 4
-7
+6
 ) %7B%0A
+%09%09%09// DefaultEditorConfigProperties.INSTANCE.getDefaultEditorConfigProperties() renamed to .getEditorConfigProperties()%0A
 %09%09%09this.
@@ -2150,33 +2150,33 @@
 tLintCompat0Dot4
-7
+6
 Dot0Adapter();%0A%09
@@ -2187,37 +2187,74 @@
 lse if (
-minorVersion %3E= 46) %7B
+version.equals(%220.45.2%22)) %7B%0A%09%09%09// add editorConfigOverride
 %0A%09%09%09this
@@ -2277,37 +2277,37 @@
 tLintCompat0Dot4
-6Dot0
+5Dot2
 Adapter();%0A%09%09%7D e
@@ -2332,24 +2332,81 @@
 on %3E= 34) %7B%0A
+%09%09%09// KtLint.INSTANCE.format() now needs more parameters%0A
 %09%09%09this.adap
@@ -2483,16 +2483,82 @@
 = 32) %7B%0A
+%09%09%09// rename packages from %60com.github.shyiko%60 to %60com.pinterest%60%0A
 %09%09%09this.
@@ -2615,16 +2615,29 @@
  else %7B%0A
+%09%09%09// the OG%0A
 %09%09%09this.

Add comments for what the differences are in each version of the adapter function.