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

<Route
  @name={{routeName}}
as |route|>
  <DataLoader
    @src={{
      uri '/${partition}/${nspace}/${dc}/license'
      (hash
        partition=route.params.partition
        nspace=route.params.nspace
        dc=route.params.dc
      )
    }}
  as |loader|>

{{#let
  loader.data
as |item|}}
    <BlockSlot @name="error">
      <ErrorState
        @error={{loader.error}}
        @login={{route.model.app.login.open}}
      />
    </BlockSlot>

    <BlockSlot @name="disconnected" as |after|>
      {{#if (eq loader.error.status "404")}}
        <Hds::Toast @color='warning'
          {{notification
            sticky=true
          }} as |T|>
          <T.Title>Warning!</T.Title>
          <T.Description>This service has been deregistered and no longer exists in the catalog.</T.Description>
        </Hds::Toast>
      {{else if (eq loader.error.status "403")}}
        <Hds::Toast @color='critical'
          {{notification
            sticky=true
          }} as |T|>
          <T.Title>Error!</T.Title>
          <T.Description>You no longer have access to this service.</T.Description>
        </Hds::Toast>
      {{else}}
        <Hds::Toast @color='warning'
          {{notification
            sticky=true
          }} as |T|>
          <T.Title>Warning!</T.Title>
          <T.Description>An error was returned whilst loading this data, refresh to try again.</T.Description>
        </Hds::Toast>
      {{/if}}
    </BlockSlot>

    <BlockSlot @name="loaded">
      <div class="tab-section">
        <section
          class={{class-map
            'validity'
            (array 'valid' item.Valid)
          }}
        >
          <header>
            <h2>
              {{compute (fn route.t 'expiry.header')}}
            </h2>
          </header>

          <p>
            {{compute (fn route.t 'expiry.${type}.body'
              (hash
                type=(if item.Valid 'valid' 'expired')
                date=(format-time item.License.expiration_time
                  year='numeric'
                  month='long'
                  day='numeric'
                )
                time=(format-time item.License.expiration_time
                  hour12=true
                  hour='numeric'
                  hourCycle='h12'
                  minute='numeric'
                  second='numeric'
                  timeZoneName='short'
                )
                htmlSafe=true
              )
            )}}
          </p>

          <dl>
            <dt class={{class-map
                (array 'valid' item.Valid)
                (array 'expired' (not item.Valid))
                (array 'warning' (temporal-within item.License.expiration_time 2629800000))
              }}
            >
              {{compute (fn route.t 'expiry.${type}.header'
                (hash
                  type=(if item.Valid 'valid' 'expired')
                )
              )}}
            </dt>
            <dd>
              {{temporal-format item.License.expiration_time}}
            </dd>
          </dl>

          <aside>
            <header>
              <h3>
                {{compute (fn route.t 'documentation.title')}}
              </h3>
            </header>
            <div class="flex gap-1.5 flex-col">
              <Hds::Link::Standalone
                @text={{compute (fn route.t 'documentation.links.license-expiration.text')}}
                @href='{{concat (env 'CONSUL_DOCS_URL') (compute (fn route.t 'documentation.links.license-expiration.link'))}}'
                @icon='docs-link'
                @iconPosition='trailing'
              />
              <Hds::Link::Standalone
                @text={{compute (fn route.t 'documentation.links.renewing-license.text')}}
                @href='{{concat (env 'CONSUL_DOCS_URL') (compute (fn route.t 'documentation.links.renewing-license.link'))}}'
                @icon='docs-link'
                @iconPosition='trailing'
              />
              <Hds::Link::Standalone
                @text={{compute (fn route.t 'documentation.links.applying-new-license.text')}}
                @href='{{concat (env 'CONSUL_DOCS_URL') (compute (fn route.t 'documentation.links.applying-new-license.link'))}}'
                @icon='docs-link'
                @iconPosition='trailing'
              />
            </div>
          </aside>

        </section>
      </div>
    </BlockSlot>
{{/let}}
  </DataLoader>
</Route>

