modules/VersionChecker.psm1

using module '.\Enums.psm1'
using module '.\Config.psm1'
using module '.\Module.psm1'
using module '.\Instance.psm1'

class VersionChecker{

    [Instance[]] $instances
    [hashtable] $applicationModules

    VersionChecker([Config] $config, [int] $tokenIatCorrectionMilliseconds, [bool]$forceSecurityProtocol){
        $this.instances = @()
        $this.applicationModules = @{}

        foreach ($operator in $config.endpoints.GetEnumerator()){
            #Write-Host ("Operator: {0}, {1}" -f @($operator.Key, $operator.Value))
            foreach ($environment in $operator.Value.PSObject.Properties) {
                #Write-Host ("Environment: {0}, {1}" -f @($environment.Name, $environment.Value))
                $brandDomain = $environment.Value.domain

                $checker = $environment.Value.versionChecker
                if ($null -ne $checker.brand){
                    $allBrands = $checker.brand

                    #Write-Host ("Checking brands...")
                    foreach ($brand in $allBrands.PSObject.Properties) {
                        if ($brand.Value.domain) { $brandDomain = $brand.Value.domain }

                        #Write-Host ("Brand: {0}" -f $brand.Name)
                        $this.instances += [Instance]::new([OPERATOR]($operator.Name), $brand.Name, [ENVIRONMENT]($environment.Name), `
                            $brandDomain, $brand.Value.key, $brand.Value.secret, $brand.Value.modules, $brand.Value.extension, `
                            $tokenIatCorrectionMilliseconds)
                    }
                }
                else {
                    $this.instances += [Instance]::new([OPERATOR]($operator.Name), '', [ENVIRONMENT]($environment.Name), `
                        $brandDomain, $checker.key, $checker.secret, $checker.modules, $checker.extension, `
                        $tokenIatCorrectionMilliseconds)
                }
            }
        }

        foreach ($key in $config.modules.Keys) {
            $this.applicationModules.Add($key, [ApplicationModule]::new($key, $config.modules[$key], $forceSecurityProtocol))
        }
    }

    [Instance[]] FindInstances([OPERATOR[]] $operators, [ENVIRONMENT[]] $environments, [string[]] $brands, [MODULE[]] $modules){
        $retValue = @()

        foreach ($instance in $this.instances){
            if (($null -eq $operators -or $instance.operator -in $operators) `
                -and ($null -eq $environments -or $instance.environment -in $environments) `
                -and ($null -eq $brands -or $instance.brand -in $brands)){

                foreach ($m in $modules) {
                    if ($m -in $instance.modules) { $retValue += $instance; break }
                }
            }
        }

        return $retValue
    }

    [MODULE[]] GetModulesWithSubmodule(){
        $retValue = @()

        foreach ($key in $this.applicationModules.Keys){
            if ($this.applicationModules[$key].containSubmodules){
                $retValue += $this.applicationModules[$key].type
            }
        }

        return $retValue
    }
}