{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
}}

<StateChart
  @src={{this.chart}}
  @initial={{if (eq @type 'oidc') 'provider' 'secret'}}
as |State Guard Action dispatch state|>
  <Guard
    @name="isSecret"
    @cond={{this.isSecret}}
  />
  {{#let
    (uri '/${partition}/${nspace}/${dc}'
      (hash
        partition=(or @value.Partition @partition)
        nspace=(or @value.Namespace @nspace)
        dc=@dc
      )
    )
  as |path|}}
    <State @matches="secret">
      <DataSource
        @src={{uri (concat path '/token/self/${value}')
          (hash
            value=@value
          )
        }}
        @onchange={{this.change}}
        @onerror={{@onerror}}
      />
    </State>
    <State @matches="provider">
      <DataSource
        @src={{uri (concat path '/oidc/provider/${value}')
          (hash
            value=@value.Name
          )
        }}
        @onchange={{queue (action (mut this.provider) value="data") (action dispatch "SUCCESS")}}
        @onerror={{@onerror}}
      />
    </State>
    <State @matches="jwt">
      <JwtSource
        @src={{this.provider.AuthURL}}
        @onchange={{queue (action (mut this.jwt) value="data") (action dispatch "SUCCESS")}}
        @onerror={{@onerror}}
      />
    </State>
    <State @matches="token">
      <DataSource
        @src={{uri (concat path '/oidc/authorize/${provider}/${code}/${state}')
            (hash
              provider=this.provider.Name
              code=this.jwt.authorizationCode
              state=(or this.jwt.authorizationState '')
            )
        }}
        @onchange={{this.change}}
        @onerror={{@onerror}}
      />
    </State>
  {{/let}}
</StateChart>