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

{{#let
  @list
  (if @partition (hash Name=@partition) (hash Name="default"))
  (is-href "dc.partitions" @dc.Name)
  (can "choose partitions" dc=@dc)
  as |SNL partition isManaging canChoose|
}}
  <SNL.Title class="consul-side-nav__selector-title">{{t
      "components.hashicorp-consul.side-nav.partitions.title"
    }}</SNL.Title>
  <NavSelector
    @list={{@list}}
    @items={{sort-by "Name:asc" (reject-by "DeletedAt" @partitions)}}
    @item={{partition}}
    @key="Name"
    @icon="users"
    @placeholder={{t
      "components.hashicorp-consul.side-nav.partitions.placeholder"
    }}
    @footerLink={{href-to "dc.partitions" @dc.Name}}
    @footerLinkText={{t
      "components.hashicorp-consul.side-nav.partitions.footer"
    }}
    @disabled={{not canChoose}}
    data-test-datacenter-menu
    as |Selector|
  >
    <Selector.Data>
      <DataSource
        @src={{uri "/*/*/${dc}/partitions" (hash dc=@dc.Name)}}
        @loading="lazy"
        @onchange={{fn (optional @onchange)}}
      />
    </Selector.Data>
    {{#if canChoose}}
      <Selector.Dropdown.Checkmark
        @selected={{eq partition.Name Selector.item.Name}}
        @href={{if
          Selector.item.href
          Selector.item.href
          (href-to
            "dc.services.index"
            params=(hash partition=Selector.item.Name nspace=undefined peer=undefined dc=@dc.Name)
          )
        }}
        @isHrefExternal={{false}}
        data-test-partiton-item
      >
        {{Selector.item.Name}}
      </Selector.Dropdown.Checkmark>
    {{/if}}
  </NavSelector>
{{/let}}