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

<Route
  @name={{this.routeName}}
as |route|>
  <DataSource
    @src={{uri '/${partition}/${nspace}/${dc}/kv/${key}'
      (hash
        partition=route.params.partition
        nspace=route.params.nspace
        dc=route.params.dc
        key=(or route.params.key '/')
      )
    }}
    @onchange={{action (mut this.parent) value="data"}}
  />
  <DataLoader
    @src={{uri '/${partition}/${nspace}/${dc}/kvs/${key}'
      (hash
        partition=route.params.partition
        nspace=route.params.nspace
        dc=route.params.dc
        key=(or route.params.key '/')
      )}}
    as |loader|>

    <BlockSlot @name="error">
      <AppError
        @error={{loader.error}}
        @login={{route.model.app.login.open}}
      />
    </BlockSlot>
      <BlockSlot @name="disconnected" as |after|>
        {{#if (eq loader.error.status "404")}}
          <Hds::Toast
            @color='warning'
            {{notification sticky=true}}
            as |T|>
            <T.Title>Warning!</T.Title>
            <T.Description>
              This KV or parent of this KV was deleted.
            </T.Description>
          </Hds::Toast>
        {{else if (eq loader.error.status "403")}}
          <Hds::Toast
            @color='critical'
            {{notification sticky=true}}
            as |T|>
            <T.Title>Error!</T.Title>
            <T.Description>
              You no longer have access to this KV.
            </T.Description>
          </Hds::Toast>
        {{else}}
          <Hds::Toast
            @color='warning'
            {{notification sticky=true}}
            as |T|>
            <T.Title>Warning!</T.Title>
            <T.Description>
              An error was returned whilst loading this data, refresh to try again.
            </T.Description>
          </Hds::Toast>
        {{/if}}
      </BlockSlot>

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

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

  (hash
    kind=(hash
      value=(if this.kind (split this.kind ',') undefined)
      change=(action (mut this.kind) value="target.selectedItems")
    )
  )

  this.parent
  loader.data

as |sort filters parent items|}}
    <AppView>
    {{#if (not-eq parent.Key '/') }}
      <BlockSlot @name="breadcrumbs">
        <ol>
          <li><a href={{href-to 'dc.kv'}}>Key / Values</a></li>
    {{#each (slice 0 -2 (split parent.Key '/')) as |breadcrumb index|}}
          <li><a {{tooltip breadcrumb}} href={{href-to 'dc.kv.folder' (join '/' (append (slice 0 (add index 1) (split parent.Key '/')) ''))}}>{{breadcrumb}}</a></li>
    {{/each}}
        </ol>
      </BlockSlot>
    {{/if}}
      <BlockSlot @name="header">
          <h1>
            {{#if (eq parent.Key '/')}}
              <route.Title @title="Key / Value" />
            {{else}}
              <route.Title @title={{take 1 (drop 1 (reverse (split parent.Key '/')))}} />
            {{/if}}
          </h1>
          <label for="toolbar-toggle"></label>
      </BlockSlot>
      <BlockSlot @name="toolbar">
    {{#if (gt items.length 0) }}
        <Consul::Kv::SearchBar
          @search={{this.search}}
          @onsearch={{action (mut this.search) value="target.value"}}

          @sort={{sort}}

          @filter={{filters}}
        />
    {{/if}}
      </BlockSlot>
      <BlockSlot @name="actions">
{{#if (can 'create kvs')}}
    {{#if (and parent.Key (not-eq parent.Key '/')) }}
      <Hds::Button
        @text='Create'
        @isHrefExternal={{false}}
        @href="{{href-to 'dc.kv.create' parent.Key}}"
        data-test-create
      />
    {{else}}
      <Hds::Button
        @text='Create'
        @route='dc.kv.root-create'
        data-test-create
      />
    {{/if}}
{{/if}}
      </BlockSlot>
      <BlockSlot @name="content">
        {{#let (refresh-route) as |refreshRoute|}}
        <DataWriter
          @sink={{uri '/${partition}/${nspace}/${dc}/kv/'
            (hash
              partition=route.params.partition
              nspace=route.params.nspace
              dc=route.params.dc
            )
          }}
          @type="kv"
          @label="key"
          @ondelete={{refreshRoute}}
          as |writer|>
          <BlockSlot @name="content">
            <DataCollection
              @type="kv"
              @sort={{sort.value}}
              @filters={{filters}}
              @search={{this.search}}
              @items={{items}}
            as |collection|>
              <collection.Collection>
                <Consul::Kv::List
                  @items={{collection.items}}
                  @parent={{parent}}
                  @delete={{writer.delete}}
                />
              </collection.Collection>
              <collection.Empty>
                <EmptyState
                  @login={{route.model.app.login.open}}
                >
                  <BlockSlot @name="header">
                    <h2>
                      {{t 'routes.dc.kv.index.empty.header'
                        items=items.length
                      }}
                    </h2>
                  </BlockSlot>
                  <BlockSlot @name="body">
                    {{t 'routes.dc.kv.index.empty.body'
                      items=items.length
                      canUseACLs=(can "use acls")
                      htmlSafe=true
                    }}
                  </BlockSlot>
                  <BlockSlot @name="actions">
                    <li>
                      <Hds::Link::Standalone
                        @text='Documentation on K/V'
                        @href="{{env 'CONSUL_DOCS_URL'}}/agent/kv"
                        @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/kv"
                        @icon='learn-link'
                        @iconPosition='trailing'
                        @size='small'
                      />
                    </li>
                  </BlockSlot>
                </EmptyState>
              </collection.Empty>
            </DataCollection>
          </BlockSlot>
        </DataWriter>
        {{/let}}
      </BlockSlot>
    </AppView>
{{/let}}
    </BlockSlot>
  </DataLoader>
</Route>
