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

{{#if (can 'use acls')}}
  <li data-test-main-nav-auth>

    <AuthDialog
      @src={{uri 'settings://consul:token'}}
      @sink={{uri 'settings://consul:token'}}
      @onchange={{this.reauthorize}}
    >
      <:unauthorized as |authDialog|>
        <Portal @target="app-before-skip-links">
          <Action
            {{on "click" (optional this.modal.open)}}
          >
            Login
          </Action>
        </Portal>
        <Action
          {{on "click" (optional this.modal.open)}}
        >
          Log in
        </Action>
        <ModalDialog
          @name="login-toggle"
          @onclose={{this.close}}
          @onopen={{this.open}}
          @aria={{hash
            label="Log in to Consul"
          }}
        as |modal|>
          <Ref 
            @target={{this}}
            @name="modal"
            @value={{modal}}
          />
          <BlockSlot @name="header">
            <h2>
              Log in to Consul
            </h2>
          </BlockSlot>
          <BlockSlot @name="body">
            <AuthForm
              @dc={{@dc.Name}}
              @partition={{@partition}}
              @nspace={{@nspace}}
              @onsubmit={{action authDialog.login value="data"}}
            as |authForm|>
              <Ref
                @target={{this}}
                @name="authForm"
                @value={{authForm}}
              />
              {{#if (can "use SSO")}}
                <authForm.Method @matches="sso">
                  <OidcSelect
                    @dc={{@dc.Name}}
                    @partition={{@partition}}
                    @nspace={{@nspace}}
                    @disabled={{authForm.disabled}}
                    @onchange={{authForm.submit}}
                    @onerror={{authForm.error}}
                  />
                </authForm.Method>
              {{/if}}
            </AuthForm>
          </BlockSlot>
          <BlockSlot @name="actions">
            <Hds::Button
              @color='secondary'
              @text='Continue without logging in'
              {{on 'click' modal.close}}
            />
          </BlockSlot>
        </ModalDialog>
      </:unauthorized>
      <:authorized as |authDialog|>
        <ModalDialog
          @name="login-toggle"
          @onclose={{this.close}}
          @onopen={{this.open}}
          @aria={{hash
            label="Log in with a different token"
          }}
        as |modal|>
          <Ref
            @target={{this}}
            @name="modal"
            @value={{modal}}
          />
          <BlockSlot @name="header">
            <h2>
              Log in with a different token
            </h2>
          </BlockSlot>
          <BlockSlot @name="body">
            <AuthForm
              @dc={{@dc.Name}}
              @nspace={{@nspace}}
              @partition={{@partition}}
              @onsubmit={{action authDialog.login value="data"}}
            as |authForm|>
              <Ref 
                @target={{this}}
                @name="authForm"
                @value={{authForm}}
              />
            </AuthForm>
          </BlockSlot>
          <BlockSlot @name="actions">
            <Hds::Button
              @color='secondary'
              @text='Continue without logging in'
              {{on 'click' modal.close}}
            />
          </BlockSlot>
        </ModalDialog>
        <Portal @target="app-before-skip-links">
          <Action
            {{on "click" (optional authDialog.logout)}}
          >
            Logout
          </Action>
        </Portal>
        <DisclosureMenu as |disclosure|>
          <disclosure.Action
            {{on 'click' disclosure.toggle}}
          >
            Logout
          </disclosure.Action>
          <disclosure.Menu as |panel|>
      {{#if authDialog.token.AccessorID}}
            <AuthProfile
              @item={{authDialog.token}}
            />
      {{/if}}
            <panel.Menu as |menu|>
              <menu.Separator />
              <menu.Item
                class="dangerous"
              >
                <menu.Action
                  {{on 'click' (optional authDialog.logout)}}
                >
                  Logout
                </menu.Action>
              </menu.Item>
            </panel.Menu>
          </disclosure.Menu>
        </DisclosureMenu>
      </:authorized>
    </AuthDialog>

  </li>
{{yield
  (hash
    open=this.modal.open
    close=this.model.close
  )
}}
{{/if}}

