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

<Route @name={{routeName}} as |route|>
  <DataLoader
    @src={{uri
      "/${partition}/${nspace}/${dc}/services/${peer}/${peerId}"
      (hash
        partition=route.params.partition
        nspace=route.params.nspace
        dc=route.params.dc
        peer=route.model.peer.Name
        peerId=route.model.peer.id
      )
    }}
    as |api|
  >

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

    <BlockSlot @name="loaded">
      {{#let
        (hash
          value=(or sortBy "Status:asc")
          change=(action (mut sortBy) value="target.selected")
        )
        (hash
          status=(hash
            value=(if status (split status ",") undefined)
            change=(action (mut status) value="target.selectedItems")
          )
          kind=(hash
            value=(if kind (split kind ",") undefined)
            change=(action (mut kind) value="target.selectedItems")
          )
          source=(hash
            value=(if source (split source ",") undefined)
            change=(action (mut source) value="target.selectedItems")
          )
          searchproperty=(hash
            value=(if
              (not-eq searchproperty undefined)
              (split searchproperty ",")
              this.searchProperties
            )
            change=(action (mut searchproperty) value="target.selectedItems")
            default=this.searchProperties
          )
        )
        (reject-by "Kind" "connect-proxy" api.data)
        (or route.params.partition route.model.user.token.Partition "default")
        (or route.params.nspace route.model.user.token.Namespace "default")
        as |sort filters items partition nspace|
      }}

        {{#if (gt items.length 0)}}
          {{#let (collection items) as |items|}}
            <Consul::Service::SearchBar
              @sources={{get items "ExternalSources"}}
              @partitions={{get items "Partitions"}}
              @partition={{partition}}
              @search={{search}}
              @onsearch={{action (mut search) value="target.value"}}
              @sort={{sort}}
              @filter={{filters}}
              @peer={{route.model.peer}}
            />
          {{/let}}
        {{/if}}
        <DataCollection
          @type="service"
          @sort={{sort.value}}
          @filters={{filters}}
          @search={{search}}
          @items={{items}}
          as |collection|
        >
          <collection.Collection>
            <Consul::Service::List
              @items={{collection.items}}
              @partition={{partition}}
              @nspace={{nspace}}
              @isPeerDetail={{true}}
            />
          </collection.Collection>
          <collection.Empty>
            <EmptyState
              @login={{route.model.app.login.open}}
              data-test-imported-services-empty
            >
              <BlockSlot @name="header">
                <h2>
                  {{t
                    "routes.dc.peers.show.imported.empty.header"
                    name=route.model.peer.Name
                  }}
                </h2>
              </BlockSlot>
              <BlockSlot @name="body">
                {{t
                  "routes.dc.peers.show.imported.empty.body"
                  items=items.length
                  name=route.model.peer.Name
                  htmlSafe=true
                }}
              </BlockSlot>
              <BlockSlot @name="actions">
                <li>
                  {{! what's the docs for peering?}}
                  <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>
          </collection.Empty>
        </DataCollection>
      {{/let}}
    </BlockSlot>
  </DataLoader>
</Route>