# shellcheck shell=dash disable=SC2120
___x_cmd_pwsh_part(){
    [ $# -gt 0 ] || set -- ls

    local op="$1";  shift
    case "$op" in
        ls|create|rm|format)
            "___x_cmd_pwsh_part_${op}" "$@" ;;
        -h|--help)
            ___x_cmd help -m pwsh part ;;
        *)
            N=pwsh M="Unsupported subcmd => $op" log:ret:64  ;;
    esac

}

# Recommend user to use system provided tool to create partition
___x_cmd_pwsh_part_create(){
    local x_output_mode=; local x_output=
    local cmd="New-Partition"
     while [ $# -gt 0 ]; do
        pwsh:output:mode
        case "$1" in
           --alignment)           cmd="${cmd} -Alignment " ;              shift 1 ;;
           --asjob)               cmd="${cmd} -Asjob " ;                  shift 1 ;;
           --assigndriveletter)   cmd="${cmd} -Asjob " ;                  shift 1 ;;
           --diskid)              cmd="${cmd} -DiskId $2 " ;              shift 2 ;;
           --disknumber)          cmd="${cmd} -DiskNumber $2 " ;          shift 2 ;;
           -p|--diskpath)         cmd="${cmd} -DiskPath $2 " ;            shift 2 ;;
           --driveletter)         cmd="${cmd} -DriveLetter $2 " ;         shift 2 ;;
           --gpttype)             cmd="${cmd} -GptType \"$2\" " ;         shift 2 ;;
           --isactive)            cmd="${cmd} -IsActive  " ;              shift 1 ;;
           --ishidden)            cmd="${cmd} -IsHidden  " ;              shift 1 ;;
           --mbrtype)             cmd="${cmd} -Mbrtype $2 " ;             shift 2 ;;
           --offset)              cmd="${cmd} -Offect $2 " ;              shift 2 ;;
           --size)                cmd="${cmd} -Size $2 " ;                shift 2 ;;
           --throttlelimit)       cmd="${cmd} -ThrottleLimit $2 " ;       shift 2 ;;
           --usemaximumsize)      cmd="${cmd} -UseMaximumSize $2 " ;      shift 2 ;;
           *)                     N=pwsh M="Unsupported option => $1" log:ret:64  ;;
        esac
    done

    ___x_cmd_pwsh___exec_admin "$x_output_mode" "$cmd" "$x_output"
}

___x_cmd_pwsh_part_rm(){
    local cmd="Remove-Partition"
     while [ $# -gt 0 ]; do
        case "$1" in
            --asjob)               cmd="${cmd} -Asjob " ;                  shift 1 ;;
            --diskid)          cmd="${cmd} -DiskId $2 " ;              shift 2 ;;
            --disknumber)      cmd="${cmd} -DiskNumber $2 " ;          shift 2 ;;
            --driveletter)         cmd="${cmd} -DriveLetter $2 " ;         shift 2 ;;
            --offect)              cmd="${cmd} -Offect $2 " ;              shift 2 ;;
            --partitionnumber)     cmd="${cmd} -PartitionNumber $2 " ;     shift 2 ;;
            --throttlelimit)       cmd="${cmd} -ThrottleLimit $2 " ;       shift 2 ;;
            --passthru)            cmd="${cmd} -PassThru $2 " ;            shift 2 ;;
            *)                    N=pwsh M="Unsupported option => $1" log:ret:64  ;;
        esac
    done

    ___x_cmd_pwsh___exec_admin "$x_output_mode" "$cmd" "$x_output"
}

___x_cmd_pwsh_part_format(){
    local x_output_mode=; local x_output=; local IS_FORCE=
    local cmd="Format-Volume"
     while [ $# -gt 0 ]; do
        pwsh:output:mode
        case "$1" in
            --allocationunitsize)   cmd="${cmd} -AllocationUnitSize  $2 " ;    shift 2 ;;
            --asjob)                cmd="${cmd} -Asjob " ;                     shift 1 ;;
            --compress)             cmd="${cmd} -Compress " ;                  shift 1 ;;
            --disableheatgathering) cmd="${cmd} -DisableHeatGathering " ;      shift 1 ;;
            --driveletter)          cmd="${cmd} -DriveLetter $2 " ;            shift 2 ;;
            --filesystem)           cmd="${cmd} -FileSystem $2 " ;             shift 2 ;;
            --filesystemlabel)      cmd="${cmd} -FileSystemLabel $2 " ;        shift 2 ;;
            --full)                 cmd="${cmd} -Full  " ;                     shift 1 ;;
            --newfilesystemlabel)   cmd="${cmd} -NewFileSystemLabel $2 " ;     shift 2 ;;
            --path)                 cmd="${cmd} -Path $2 " ;                   shift 2 ;;
            --setintegritystreams)  cmd="${cmd} -SetIntegrityStreams $2 " ;    shift 2 ;;
            --shortfilenamesupport) cmd="${cmd} -SShortFileNameSupport $2 " ;  shift 2 ;;
            --uselargefrs)          cmd="${cmd} -UseLargeFRS $2 " ;            shift 2 ;;
            -f|--force)             IS_FORCE=1;                                shift 1 ;;
            *)                      N=pwsh M="Unsupported option => $1"    log:ret:64  ;;
        esac
    done

    [ -n "$IS_FORCE" ] || ___x_cmd ui yesno "Are you sure you want to format partition?" || return $?
    ___x_cmd_pwsh___exec_admin "$x_output_mode" "$cmd" "$x_output"
}

___x_cmd_pwsh_part_ls(){
    local x_output_mode=; local x_output=

    pwsh:arg:null

    while [ $# -gt 0 ]; do
        pwsh:output:mode:all
    done

    [ "$x_output_mode" != app ] || {
        ___x_cmd_pwsh___exec "$x_output_mode" "Get-Partition" "$x_output" \
            | ___x_cmd csv tab - 12,27,13,28,2 \
            | ___x_cmd csv --app
        return
    }

    ___x_cmd_pwsh___exec "$x_output_mode" "Get-Partition" "$x_output"
}
