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

<Route
  @name={{routeName}}
as |route|>
  <DataLoader
    @src={{
      uri '/${partition}/${nspace}/${dc}/datacenter'
      (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
              'server-failure-tolerance'
            }}
          >

            <header>
              <h2>
                {{compute (fn route.t 'tolerance.header')}}
              </h2>
              <Hds::Link::Standalone
                @icon='docs-link'
                @iconPosition='trailing'
                @text={{compute (fn route.t 'tolerance.link-text')}}
                @href='{{concat (env 'CONSUL_DOCS_URL') (compute (fn route.t 'tolerance.link'))}}'
              />
            </header>

            <section
              class={{class-map
                (array 'immediate-tolerance')
              }}
            >
              <header>
                <h3>
                  {{compute (fn route.t 'tolerance.immediate.header')}}
                </h3>
              </header>
              <dl
                class={{class-map
                  (array 'warning' (and
                    (eq item.FailureTolerance 0)
                    (eq item.OptimisticFailureTolerance 0)
                  ))
                }}
              >
                <dt>
                  {{compute (fn route.t 'tolerance.immediate.body')}}
                </dt>
                <dd>
                  {{item.FailureTolerance}}
                </dd>
              </dl>
            </section>

              {{#if (can 'read zones')}}
            <section
              class={{class-map
                (array 'optimistic-tolerance')
              }}
            >
              <header>
                <h3>
                  {{compute (fn route.t 'tolerance.optimistic.header')}}
                  <span
                    {{tooltip 'With > 30 seconds between server failures, Consul can restore the Immediate Fault Tolerance by replacing failed active voters with healthy back-up voters when using redundancy zones.'}}
                  >
                  </span>
                </h3>
              </header>
              <dl
                class={{class-map
                  (array 'warning' (eq item.OptimisticFailureTolerance 0))
                }}
              >
                <dt>
                  {{compute (fn route.t 'tolerance.optimistic.body')}}
                </dt>
                <dd>
                  {{item.OptimisticFailureTolerance}}
                </dd>
              </dl>

            </section>
              {{/if}}
          </section>

    {{#if (gt item.RedundancyZones.length 0)}}
          <section
            class={{class-map
              'redundancy-zones'
            }}
          >
            <header>
              <h2>
                {{pluralize (t 'common.consul.redundancyzone')}}
              </h2>
            </header>

      {{#each item.RedundancyZones as |item|}}
        {{#if (gt item.Servers.length 0) }}
              <section>
                <header>
                  <h3>
                    {{item.Name}}
                  </h3>
                  <dl
                    class={{class-map
                      (array 'warning' (eq item.FailureTolerance 0))
                    }}
                  >
                      <dt
                      >{{t 'common.consul.failuretolerance'}}</dt>
                      <dd>{{item.FailureTolerance}}</dd>
                  </dl>
                </header>
                <Consul::Server::List
                  @items={{item.Servers}}
                />
              </section>
        {{/if}}
      {{/each}}

      {{#if (gt item.Default.Servers.length 0)}}
              <section>
                <header>
                  <h3>
                    {{compute (fn route.t 'unassigned')}}
                  </h3>
                </header>
                <Consul::Server::List
                  @items={{item.Default.Servers}}
                />
              </section>
      {{/if}}

          </section>
    {{else}}
          <section>
            <header>
              <h2>
                {{compute (fn route.t 'servers')}}
              </h2>
            </header>
            <Consul::Server::List
              @items={{item.Default.Servers}}
            />
          </section>
    {{/if}}

    {{#if (gt item.ReadReplicas.length 0)}}
          <section>
            <header>
              <h2>
                {{pluralize (t 'common.consul.readreplica')}}
              </h2>
            </header>

            <Consul::Server::List
              @items={{item.ReadReplicas}}
            />
          </section>
    {{/if}}

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