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

<Route
  @name={{routeName}}
as |route|>

{{#let
  '/'
as |separator|}}

{{#let
  (concat (join separator (slice 0 -1 (split route.params.key separator))) separator)
as |parentKey|}}

  <DataLoader
    @src={{uri '/${partition}/${nspace}/${dc}/kv/${key}'
      (hash
        partition=route.params.partition
        nspace=route.params.nspace
        dc=route.params.dc
        key=(if (string-ends-with routeName 'create') '' route.params.key)
      )
    }}
  as |loader|>

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

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

  route.params.dc
  route.params.partition
  route.params.nspace
  loader.data

as |dc partition nspace item|}}

  <AppView>

    <BlockSlot @name="breadcrumbs">
      <ol>
        <li>
          <Action
            data-test-back
            @href={{href-to 'dc.kv.index'}}
          >
            Key / Values
          </Action>
        </li>
{{#if (not-eq parentKey separator)}}

{{#let
  (split parentKey separator)
as |parts|}}

    {{#each parts as |breadcrumb index|}}
      {{#if (gt breadcrumb.length 0)}}
        {{! if breadcrumb isn't '' then slice of enough of the parentKey }}
        {{! to make the correct href. 'Enough' is the current index plus 1.}}
        {{! We push on a '' here so make sure we get a trailing slash/separator }}
          <li>
            <Action
              {{action breadcrumb}}
              @href={{href-to 'dc.kv.folder'
                  (join '/'
                    (append
                      (slice 0 (add index 1) parts) ''
                    )
                  )
              }}
            >
              {{breadcrumb}}
            </Action>
          </li>
      {{/if}}
    {{/each}}
{{/let}}

{{/if}}
      </ol>
    </BlockSlot>

    <BlockSlot @name="header">
        <h1 data-test-kv-key={{item.Key}}>
{{#if (and item.Key (not-eq item.Key parentKey))}}
          <route.Title @title="Edit Key / Value" @render={{false}} />
          {{left-trim item.Key parentKey}}
{{else}}
          <route.Title @title="New Key / Value" @render={{true}} />
{{/if}}
        </h1>
    </BlockSlot>

    <BlockSlot @name="content">

{{! if a KV has a session `Session` will always be populated despite any specific session permissions }}
{{#if item.Session}}
    <Consul::LockSession::Notifications @type="kv" />
{{/if}}

      <Consul::Kv::Form
        @item={{item}}
        @dc={{route.params.dc}}
        @nspace={{route.params.nspace}}
        @partition={{route.params.partition}}
        @onsubmit={{if (eq parentKey separator) (transition-to 'dc.kv.index') (transition-to 'dc.kv.folder' parentKey)}}
        @parent={{parentKey}}
      />


{{! `session` is slightly different to `item.Session` as we only have `session` }}
{{! if you have `session:read perms` whereas you can get the sessions ID from }}
{{! `item.Session` without any session perms }}
{{#if (and item.Session)}}
      <DataSource
        @src={{uri '/${partition}/${nspace}/${dc}/sessions/for-key/${id}'
            (hash
              partition=route.params.partition
              nspace=route.params.nspace
              dc=route.params.dc
              id=item.Session
            )
        }}
        @onchange={{action (mut session) value="data"}}
      />
      <h2>
        <Action
          rel="help"
          @href={{concat (env 'CONSUL_DOCS_URL') '/internals/sessions.html#session-design'}}
          @external={{true}}
        >
          Lock Session
        </Action>
      </h2>
  {{#if session.ID}}
      <Consul::LockSession::Form
        @item={{session}}
        @ondelete={{loader.invalidate}}
      />
  {{/if}}
{{/if}}

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