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

<Route
  @name={{routeName}}
as |route|>
  <DataLoader
    @src={{
      uri '/${partition}/${nspace}/${dc}/namespaces'
      (hash
        partition=route.params.partition
        nspace=route.params.nspace
        dc=route.params.dc
      )}}
    as |loader|>

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

    <BlockSlot @name="loaded">
  {{#let

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

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

    loader.data

  as |sort filters items|}}

    <AppView>
      <BlockSlot @name="header">
        <h1>
          <route.Title @title="Namespaces" />
        </h1>
      </BlockSlot>
      <BlockSlot @name="actions">
  {{#if (can "create nspaces")}}
    <Hds::Button
      @text='Create'
      @route='dc.nspaces.create'
      data-test-create
    />
  {{/if}}
      </BlockSlot>
      <BlockSlot @name="toolbar">
      {{#if (gt items.length 0)}}
        <Consul::Nspace::SearchBar
          @search={{search}}
          @onsearch={{action (mut search) value="target.value"}}

          @sort={{sort}}

          @filter={{filters}}
        />
      {{/if}}
      </BlockSlot>
      <BlockSlot @name="content">
        {{#let (refresh-route) as |refreshRoute|}}
        <DataWriter
          @sink={{uri '/${partition}/${dc}/${nspace}/nspace/'
            (hash
              partition=route.params.partition
              nspace=route.params.nspace
              dc=route.params.dc
            )
          }}
          @type="nspace"
          @label="Namespace"
          @ondelete={{refreshRoute}}
        as |writer|>
          <BlockSlot @name="removed" as |after|>
            <Consul::Nspace::Notifications 
              {{notification
                after=(action after)
              }}
              @type="remove"
            />
          </BlockSlot>
          <BlockSlot @name="content">
        <DataCollection
          @type="nspace"
          @sort={{sort.value}}
          @filters={{filters}}
          @search={{search}}
          @items={{items}}
        as |collection|>
          <collection.Collection>
            <Consul::Nspace::List
              @items={{collection.items}}
              @ondelete={{writer.delete}}
            />
          </collection.Collection>
          <collection.Empty>
            <EmptyState
              @login={{route.model.app.login.open}}
            >
              <BlockSlot @name="header">
                <h2>
                  {{t 'routes.dc.namespaces.index.empty.header'
                    items=items.length}}
                </h2>
              </BlockSlot>
              <BlockSlot @name="body">
                <p>
                  {{t 'routes.dc.namespaces.index.empty.body'
                    items=items.length
                    canUseACLs=(can 'use acls')}}
                </p>
              </BlockSlot>
              <BlockSlot @name="actions">
                <li>
                  <Hds::Link::Standalone
                    @text='Documentation on namespaces'
                    @href="{{env 'CONSUL_DOCS_URL'}}/commands/namespace"
                    @icon='docs-link'
                    @iconPosition='trailing'
                    @size='small'
                  />
                </li>
                <li>
                  <Hds::Link::Standalone
                    @text='Read the guide'
                    @href="{{env 'CONSUL_DOCS_LEARN_URL'}}/consul/namespaces/secure-namespaces"
                    @icon='learn-link'
                    @iconPosition='trailing'
                    @size='small'
                  />
                </li>
              </BlockSlot>
            </EmptyState>
          </collection.Empty>
        </DataCollection>
    </BlockSlot>
  </DataWriter>
  {{/let}}
      </BlockSlot>
    </AppView>
  {{/let}}
    </BlockSlot>
  </DataLoader>
</Route>
