// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.

package com.intellij.codeInsight.editorActions.smartEnter;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiCodeBlock;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiJavaToken;
import com.intellij.psi.PsiSwitchBlock;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;

public class MissingSwitchBodyFixer implements Fixer {
  @Override
  public void apply(Editor editor, JavaSmartEnterProcessor processor, @NotNull PsiElement psiElement)
    throws IncorrectOperationException {
    if (!(psiElement instanceof PsiSwitchBlock switchStatement)) return;

    final Document doc = editor.getDocument();

    final PsiCodeBlock body = switchStatement.getBody();
    if (body != null) return;

    final PsiJavaToken rParenth = switchStatement.getRParenth();
    assert rParenth != null;

    int offset = rParenth.getTextRange().getEndOffset();
    doc.insertString(offset, "{\n}");
    editor.getCaretModel().moveToOffset(offset);
  }
}