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

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

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

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

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

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

    api.data

  as |sort filters items|}}

    <AppView>
      <BlockSlot @name="header">
          <h1>
            <route.Title @title="Intentions" /> <em>{{format-number items.length}} total</em>
          </h1>
          <label for="toolbar-toggle"></label>
      </BlockSlot>
      <BlockSlot @name="actions">
  {{#if (can 'create intentions')}}
    <Hds::Button
      @text='Create'
      @route='dc.intentions.create'
      data-test-create
    />
  {{/if}}
      </BlockSlot>
      <BlockSlot @name="toolbar">

  {{#if (gt items.length 0) }}
        <Consul::Intention::SearchBar
          @search={{search}}
          @onsearch={{action (mut search) value="target.value"}}

          @sort={{sort}}

          @filter={{filters}}
        />
  {{/if}}

      </BlockSlot>
      <BlockSlot @name="content">
        <DataWriter
          @sink={{uri '/${partition}/${dc}/${nspace}/intention/'
            (hash
              partition=route.params.partition
              nspace=route.params.nspace
              dc=route.params.dc
            )
          }}
          @type="intention"
          @ondelete={{refresh-route}}
        as |writer|>
          <BlockSlot @name="content">
            <DataCollection
              @type="intention"
              @sort={{sort.value}}
              @filters={{filters}}
              @search={{search}}
              @items={{items}}
            as |collection|>
              <collection.Collection>
                <Consul::Intention::List
                  @items={{collection.items}}
                  @delete={{writer.delete}}
                as |list|>
                    <list.CustomResourceNotice />
                    <list.Table />
                </Consul::Intention::List>
              </collection.Collection>
              <collection.Empty>
                <EmptyState
                  @login={{route.model.app.login.open}}
                >
                  <BlockSlot @name="header">
                    <h2>
                      {{t 'routes.dc.intentions.index.empty.header'
                        items=items.length
                      }}
                    </h2>
                  </BlockSlot>
                  <BlockSlot @name="body">
                    {{t 'routes.dc.intentions.index.empty.body'
                      items=items.length
                      canUseACLs=(can "use acls")
                      htmlSafe=true
                    }}
                  </BlockSlot>
                  <BlockSlot @name="actions">
                    <li>
                      <Hds::Link::Standalone
                        @text='Documentation on intentions'
                        @href="{{env 'CONSUL_DOCS_URL'}}/commands/intention"
                        @icon='docs-link'
                        @iconPosition='trailing'
                        @size='small'
                      />
                    </li>
                    <li>
                      <Hds::Link::Standalone
                        @text='Take the tutorial'
                        @href="{{env 'CONSUL_DOCS_LEARN_URL'}}/consul/getting-started/connect"
                        @icon='learn-link'
                        @iconPosition='trailing'
                        @size='small'
                      />
                    </li>
                  </BlockSlot>
                </EmptyState>
              </collection.Empty>
            </DataCollection>
          </BlockSlot>
        </DataWriter>
      </BlockSlot>
    </AppView>

  {{/let}}
    </BlockSlot>
  </DataLoader>
</Route>