using namespace Microsoft.Win32;

param(
    $Action,
    [hashtable] $Arguments
)

. "$PSScriptRoot/../../../Common/Scripts/Software.ps1";
. "$PSScriptRoot/../../../Common/Software/PowerShell/Profile.ps1";

Start-SoftwareInstaller @PSBoundParameters `
    -Installer {
        param(
            [scriptblock] $Installer
        )

        & $Installer -Action ([InstallerAction]::Configure)
    } `
    -Configurator {
        [string] $backup = $null;
        $nativeProfile = powershell -c '$PROFILE';

        if (Test-Path -PathType Leaf $nativeProfile) {
            $backup = "${nativeProfile}_";
            Move-Item $nativeProfile $backup;
        }

        $null = New-Item -Force $nativeProfile;
        choco install -y --force chocolatey;

        Add-PowerShellProfileStatement `
            -DefaultUser `
            -Category "chocolatey" `
            -Script (Get-Content $nativeProfile | Out-String) `
            -Append;

        if ($backup) {
            Move-Item $backup $nativeProfile;
        }

        Import-Module "$env:ChocolateyInstall/helpers/chocolateyProfile.psm1";
    };