-- | Conversion of 'SolverInstallPlan' to 'InstallPlan'.
-- Similar to 'elaboratedInstallPlan'
configureInstallPlan :: SolverInstallPlan -> InstallPlan
configureInstallPlan solverPlan =
    flip mapPreservingGraph solverPlan $ \mapDep planpkg ->
      case planpkg of
        PreExisting pkg ->
          PreExisting pkg

        Configured  pkg ->
          Configured (configureSolverPackage mapDep pkg)

        _ -> error "configureInstallPlan: unexpected package state"
  where
    configureSolverPackage :: (UnitId -> UnitId)
                           -> SolverPackage UnresolvedPkgLoc
                           -> ConfiguredPackage UnresolvedPkgLoc
    configureSolverPackage mapDep spkg =
      ConfiguredPackage {
        confPkgId = SimpleUnitId
                  $ Configure.computeComponentId
                        Cabal.NoFlag
                        (packageId spkg)
                        (PD.CLibName (display (pkgName (packageId spkg))))
                        -- TODO: this is a hack that won't work for Backpack.
                        (map ((\(SimpleUnitId cid0) -> cid0) . confInstId)
                             (CD.libraryDeps deps))
                        (solverPkgFlags spkg),
        confPkgSource = solverPkgSource spkg,
        confPkgFlags  = solverPkgFlags spkg,
        confPkgStanzas = solverPkgStanzas spkg,
        confPkgDeps   = deps
      }
      where
        deps = fmap (map (configureSolverId mapDep)) (solverPkgDeps spkg)

    configureSolverId mapDep sid =
      ConfiguredId {
        confSrcId  = packageId sid, -- accurate!
        confInstId = mapDep (installedUnitId sid)
      }

-- | 'GenericInstallPlan' specialised to most commonly used types.
configureInstallPlan :: SolverInstallPlan -> InstallPlan
configureInstallPlan solverPlan =
    flip mapPreservingGraph solverPlan $ \mapDep planpkg ->
      case planpkg of
        PreExisting pkg ->
          PreExisting pkg

        Configured  pkg ->
          Configured (configureSolverPackage mapDep pkg)

        _ -> error "configureInstallPlan: unexpected package state"
  where
    configureSolverPackage :: (UnitId -> UnitId)
                           -> SolverPackage UnresolvedPkgLoc
                           -> ConfiguredPackage UnresolvedPkgLoc
    configureSolverPackage mapDep spkg =
      ConfiguredPackage {
        confPkgId = SimpleUnitId
                  $ Configure.computeComponentId
                        Cabal.NoFlag
                        (packageId spkg)
                        (PD.CLibName (display (pkgName (packageId spkg))))
                        -- TODO: this is a hack that won't work for Backpack.
                        (map ((\(SimpleUnitId cid0) -> cid0) . confInstId)
                             (CD.libraryDeps deps))
                        (solverPkgFlags spkg),
        confPkgSource = solverPkgSource spkg,
        confPkgFlags  = solverPkgFlags spkg,
        confPkgStanzas = solverPkgStanzas spkg,
        confPkgDeps   = deps
      }
      where
        deps = fmap (map (configureSolverId mapDep)) (solverPkgDeps spkg)

    configureSolverId mapDep sid =
      ConfiguredId {
        confSrcId  = packageId sid, -- accurate!
        confInstId = mapDep (installedUnitId sid)
      }

-- | 'GenericInstallPlan' specialised to most commonly used types.
configureInstallPlan solverPlan =
    flip mapPreservingGraph solverPlan $ \mapDep planpkg ->
      case planpkg of
        PreExisting pkg ->
          PreExisting pkg

        Configured  pkg ->
          Configured (configureSolverPackage mapDep pkg)

        _ -> error "configureInstallPlan: unexpected package state"
  where
    configureSolverPackage :: (UnitId -> UnitId)
                           -> SolverPackage UnresolvedPkgLoc
                           -> ConfiguredPackage UnresolvedPkgLoc
    configureSolverPackage mapDep spkg =
      ConfiguredPackage {
        confPkgId = SimpleUnitId
                  $ Configure.computeComponentId
                        Cabal.NoFlag
                        (packageId spkg)
                        (PD.CLibName (display (pkgName (packageId spkg))))
                        -- TODO: this is a hack that won't work for Backpack.
                        (map ((\(SimpleUnitId cid0) -> cid0) . confInstId)
                             (CD.libraryDeps deps))
                        (solverPkgFlags spkg),
        confPkgSource = solverPkgSource spkg,
        confPkgFlags  = solverPkgFlags spkg,
        confPkgStanzas = solverPkgStanzas spkg,
        confPkgDeps   = deps
      }
      where
        deps = fmap (map (configureSolverId mapDep)) (solverPkgDeps spkg)

    configureSolverId mapDep sid =
      ConfiguredId {
        confSrcId  = packageId sid, -- accurate!
        confInstId = mapDep (installedUnitId sid)
      }

-- | 'GenericInstallPlan' specialised to most commonly used types.