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

<Route
  @name={{this.routeName}}
as |route|>
  <DataLoader
    @src={{uri '/${partition}/${nspace}/${dc}/intentions/for-service/${slug}'
      (hash
        partition=route.params.partition
        nspace=route.params.nspace
        dc=route.params.dc
        slug=route.params.name
      )
    }}
  as |api|>
    <BlockSlot @name="error">
      <ErrorState @error={{api.error}} />
    </BlockSlot>
    <BlockSlot @name="loaded">
  {{#let

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

    (hash
      access=(hash
        value=(if this.access (split this.access ',') undefined)
        change=(action (mut this.access) 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
      )
    )

    api.data
    route.model.item

  as |sort filters items item|}}
    <div class="tab-section">
  {{#if (can 'create intention for service' item=item.Service)}}
        <Portal @target="app-view-actions">
          <Hds::Button
            @text='Create'
            @route='dc.services.show.intentions.create'
            data-test-create
          />
        </Portal>
  {{/if}}
  {{#if (gt items.length 0) }}
        <Consul::Intention::SearchBar
          @search={{this.search}}
          @onsearch={{action (mut this.search) value="target.value"}}

          @sort={{sort}}

          @filter={{filters}}
        />
  {{/if}}
  {{#let (refresh-route) as |refreshRoute|}}
        <DataWriter
          @sink={{uri
            '/${partition}/${dc}/${nspace}/intention/'
            (hash
              partition=route.params.partition
              nspace=route.params.nspace
              dc=route.params.dc
            )
          }}
          @type="intention"
          @ondelete={{refreshRoute}}
        as |writer|>
          <BlockSlot @name="content">
            <DataCollection
              @type="intention"
              @sort={{sort.value}}
              @filters={{filters}}
              @search={{this.search}}
              @items={{items}}
            as |collection|>
              <collection.Collection>
                <Consul::Intention::List
                  @items={{collection.items}}
                  @check={{this.search}}
                  @delete={{writer.delete}}
                as |list|>
                  <list.CustomResourceNotice />
                  <list.CheckNotice />
                  <list.Table @routeName="dc.services.show.intentions.edit" />
                </Consul::Intention::List>
              </collection.Collection>
              <collection.Empty>
                <EmptyState
                  @login={{route.model.app.login.open}}
                >
                  <BlockSlot @name="header">
                    <h2>
                      {{t 'routes.dc.services.show.intentions.index.empty.header'
                        items=items.length
                      }}
                    </h2>
                  </BlockSlot>
                  <BlockSlot @name="body">
                    {{t 'routes.dc.services.show.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>
        {{/let}}
    </div>
  {{/let}}
    </BlockSlot>
  </DataLoader>
</Route>
