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

<Route @name={{routeName}} as |route|>
  <DataLoader
    @src={{uri
      "/${partition}/${nspace}/${dc}/exported-services/${name}"
      (hash
        partition=route.params.partition
        nspace=route.params.nspace
        dc=route.params.dc
        name=route.model.peer.Name
      )
    }}
    as |api|
  >
    {{#let
      (or route.params.partition route.model.user.token.Partition "default")
      api.data
      as |partition items|
    }}

      <BlockSlot @name="error">
        <AppError @error={{api.error}} @login={{route.model.app.login.open}} />
      </BlockSlot>

      <BlockSlot @name="loaded">
        {{#if items.length}}
          <div class="search-bar">
            <form class="filter-bar">
              <FreetextFilter
                @onsearch={{pick "target.value" this.updateSearch}}
                @value={{this.search}}
                @placeholder="Search"
                class="!w-80"
              />
            </form>
          </div>
        {{/if}}
        <Providers::Search
          @items={{items}}
          @search={{this.search}}
          @searchProperties={{array "Name"}}
          as |search|
        >
          <Providers::Dimension as |p|>
            {{#if p.data.height}}
              <div
                style={{p.data.fillRemainingHeightStyle}}
                class="overflow-y-scroll"
              >
                {{#if search.data.items.length}}
                  <VerticalCollection
                    @tagName="ul"
                    @estimateHeight={{p.data.height}}
                    @items={{search.data.items}}
                    as |service index|
                  >
                    <li
                      class="px-3 h-12 border-bottom-primary"
                    >
                      <a
                        data-test-service-name
                        class="hds-typography-display-300 hds-foreground-strong hds-font-weight-semibold h-full w-full flex items-center"
                        href={{href-to
                          "dc.services.show.index"
                          service.Name
                          params=(if
                            (not-eq service.Partition partition)
                            (hash
                              partition=service.Partition
                              nspace=service.Namespace
                              peer=service.PeerName
                            )
                            (hash peer=service.PeerName)
                          )
                        }}
                      >
                        {{service.Name}}
                      </a>
                    </li>
                  </VerticalCollection>
                {{else}}
                  <EmptyState
                    @login={{route.model.app.login.open}}
                    data-test-exported-services-empty
                  >
                    <BlockSlot @name="header">
                      <h2>
                        {{t
                          "routes.dc.peers.show.exported.empty.header"
                          name=route.model.peer.Name
                        }}
                      </h2>
                    </BlockSlot>
                    <BlockSlot @name="body">
                      {{t
                        "routes.dc.peers.show.exported.empty.body"
                        items=items.length
                        name=route.model.peer.Name
                        htmlSafe=true
                      }}
                    </BlockSlot>
                    <BlockSlot @name="actions">
                      <li>
                        <Hds::Link::Standalone
                          @text='Documentation on Peers'
                          @href="{{env
                            'CONSUL_DOCS_URL'
                          }}/connect/cluster-peering"
                          @icon='docs-link'
                          @iconPosition='trailing'
                          @size='small'
                        />
                      </li>
                      <li>
                        <Hds::Link::Standalone
                          @text='Take the tutorial'
                          @href="{{env
                            'CONSUL_DOCS_URL'
                          }}/connect/cluster-peering/create-manage-peering"
                          @icon='learn-link'
                          @iconPosition='trailing'
                          @size='small'
                        />
                      </li>
                    </BlockSlot>
                  </EmptyState>
                {{/if}}
              </div>
            {{/if}}
          </Providers::Dimension>

        </Providers::Search>
      </BlockSlot>
    {{/let}}
  </DataLoader>

</Route>