// 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.ide.impl;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.platform.DirectoryProjectConfigurator;
import org.jetbrains.annotations.NotNull;

public class NoContentRootProjectConfigurator implements DirectoryProjectConfigurator {
  @Override
  public void configureProject(@NotNull Project project,
                               final @NotNull VirtualFile baseDir,
                               final @NotNull Ref<Module> moduleRef,
                               boolean isProjectCreatedWithWizard) {
    VirtualFile projectsRoot = LocalFileSystem.getInstance().findFileByPath(ProjectUtil.getProjectPath());
    if (projectsRoot == null || !VfsUtilCore.isAncestor(projectsRoot, baseDir, true)) return;

    ApplicationManager.getApplication().runWriteAction(() -> {
      for (Module module : ModuleManager.getInstance(project).getModules()) {
        ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
        ModifiableRootModel rootModel = rootManager.getModifiableModel();
        for (ContentEntry entry : rootModel.getContentEntries()) {
          VirtualFile entryFile = entry.getFile();
          if (entryFile != null && VfsUtilCore.isAncestor(projectsRoot, entryFile, true)) {
            rootModel.removeContentEntry(entry);
          }
        }
        rootModel.inheritSdk();
        rootModel.commit();
      }
    });
  }
}
