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

<Route
  @name={{this.routeName}}
as |route|>
<div class="tab-section">
{{#let

  (hash
    value=(or this.sortBy "Status:asc")
    change=(action (mut this.sortBy) value="target.selected")
  )

  (hash
    status=(hash
      value=(if this.status (split this.status ',') undefined)
      change=(action (mut this.status) value="target.selectedItems")
    )
    source=(hash
      value=(if this.source (split this.source ',') undefined)
      change=(action (mut this.source) value="target.selectedItems")
    )
    searchproperty=(hash
      value=(if (not-eq this.searchproperty undefined)
        (split this.searchproperty ',')
        this.searchProperties
      )
      change=(action (mut this.searchproperty) value="target.selectedItems")
      default=this.searchProperties
    )
  )

  route.model.items
  route.model.proxies.firstObject

as |sort filters items proxyMeta|}}
    {{#if (gt items.length 0) }}
    <input type="checkbox" id="toolbar-toggle" />
    <Consul::ServiceInstance::SearchBar
      @sources={{get (collection items) 'ExternalSources'}}
      @search={{this.search}}
      @onsearch={{action (mut this.search) value="target.value"}}

      @sort={{sort}}

      @filter={{filters}}
      />
    {{/if}}
    {{#if proxyMeta.ServiceName}}
      <DataSource
      @src={{uri '/${partition}/${nspace}/${dc}/service-instances/for-service/${name}/${peer}'
          (hash
            partition=route.params.partition
            nspace=route.params.nspace
            dc=route.params.dc
            name=proxyMeta.ServiceName
            peer=route.params.peer
          )
        }}
        @onchange={{action (mut this.proxies) value="data"}}
      />
    {{/if}}
    {{! Service > Service Instance view doesn't require filtering of proxies }}
    <DataCollection
      @type="service-instance"
      @sort={{sort.value}}
      @filters={{filters}}
      @search={{this.search}}
      @items={{items}}
    as |collection|>
      <collection.Collection>
        <Consul::ServiceInstance::List
          @routeName="dc.services.instance"
          @items={{collection.items}}
          @proxies={{this.proxies}}
        />
      </collection.Collection>
      <collection.Empty>
        <EmptyState>
          <BlockSlot @name="body">
            {{t "routes.dc.services.show.instances.empty"
              items=items.length
              htmlSafe=true
            }}
          </BlockSlot>
        </EmptyState>
      </collection.Empty>
    </DataCollection>
{{/let}}
</div>
</Route>
