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

{{yield}}
<fieldset
  class='policy-form'
  disabled={{if (not (can 'write policy' item=this.item)) 'disabled'}}
  {{fix-super-select-aria}}
  ...attributes
>
  {{#yield-slot name='template'}}{{else}}
    <h3>
      Policy{{if this.allowIdentity ' or identity?' ''}}
    </h3>
    {{#if this.allowIdentity}}
      <p>
        Identities are default policies with configurable names. They save you some time and effort
        you're using Consul for Connect features.
      </p>
      {{! this should use radio-group }}
      <div role='radiogroup' class={{if this.item.error.Type ' has-error'}}>
        {{#each this.templates as |template|}}
          <label data-test-radiobutton={{concat 'template_' template.template}}>
            <span>{{template.name}}</span>
            <input
              type='radio'
              name={{concat this.name '[template]'}}
              value={{template.template}}
              checked={{eq this.item.template template.template}}
              onchange={{action (optional (changeset-set this.item 'template')) value='target.value'}}
            />
          </label>
        {{/each}}
      </div>
    {{else}}
      <input type='hidden' name={{concat this.name '[template]'}} value='' />
    {{/if}}
  {{/yield-slot}}
  <label class='type-text{{if (and this.item.error.Name (not this.item.isPristine)) " has-error"}}'>
    <span>Name</span>
    <input
      type='text'
      value={{this.item.Name}}
      name='{{this.name}}[Name]'
      autofocus='autofocus'
      oninput={{action 'change'}}
    />
    <em>
      Maximum 128 characters. May only include letters (uppercase and/or lowercase) and/or numbers.
      Must be unique.
    </em>
    {{#if (and this.item.error.Name (not this.item.isPristine))}}
      <strong>{{this.item.error.Name.validation}}</strong>
    {{/if}}
  </label>
  <label for='' class='type-text' data-test-rules>
    {{#if (eq this.item.template 'service-identity')}}
      <Hds::CodeBlock
        @language='hcl'
        @ariaLabel={{concat this.name '[Rules]'}}
        @value={{service-identity-template
          this.item.Name
          nspace=@nspace
          partition=@partition
          canUsePartitions=(can 'use partitions')
          canUseNspaces=(can 'use nspaces')
        }}
        as |CB|
      >
        <CB.Title @tag='span'>
          Rules
          <a
            href='{{env "CONSUL_DOCS_URL"}}/guides/acl.html#rule-specification'
            rel='help noopener noreferrer'
            target='_blank'
            style="color: inherit;"
          >(HCL Format)</a>
        </CB.Title>
      </Hds::CodeBlock>
    {{else if (eq this.item.template 'node-identity')}}
      <Hds::CodeBlock
        @language='hcl'
        @ariaLabel={{concat this.name '[Rules]'}}
        @value={{node-identity-template
          this.item.Name
          partition=@partition
          canUsePartitions=(can 'use partitions')
          canUseNspaces=(can 'use nspaces')
        }}
        as |CB|
      >
        <CB.Title @tag='span'>
          Rules
          <a
            href='{{env "CONSUL_DOCS_URL"}}/guides/acl.html#rule-specification'
            rel='help noopener noreferrer'
            target='_blank'
            style="color: inherit;"
          >(HCL Format)</a>
        </CB.Title>
      </Hds::CodeBlock>
    {{else}}
      <Hds::CodeEditor
        @value={{this.item.Rules}}
        @ariaLabel={{concat this.name '[Rules]'}}
        @syntax='hcl'
        @onInput={{action 'change' (concat this.name '[Rules]')}}
        as |CE|
      >
        <CE.Title @tag='span'>
          Rules
          <a
            href='{{env "CONSUL_DOCS_URL"}}/guides/acl.html#rule-specification'
            rel='help noopener noreferrer'
            target='_blank'
            style="color: inherit;"
          >(HCL Format)</a>
        </CE.Title>
      </Hds::CodeEditor>
      {{#if this.item.error.Rules}}
        <strong>{{this.item.error.Rules.validation}}</strong>
      {{/if}}
    {{/if}}
  </label>
  {{#if (eq this.item.template 'node-identity')}}

    <DataSource
      @src={{uri '/*/*/*/datacenters'}}
      @onchange={{action (mut this.datacenters) value='data'}}
    />
    <label class='type-select' data-test-datacenter>
      <span>Datacenter</span>
      <Hds::Form::SuperSelect::Single::Field
        @options={{map-by 'Name' this.datacenters}}
        @selected={{or this.item.Datacenter @dc}}
        @searchField="Name"
        @searchPlaceholder="Type a datacenter name"
        @onChange={{action 'change' 'Datacenter'}}
        @searchEnabled={{true}}
        @label="Datacenter"
        @listboxAriaLabel="Available datacenters"
        @isOptional={{false}}
        id="datacenter-select"
      as |F|>
        <F.Options>{{F.options}}</F.Options>
      </Hds::Form::SuperSelect::Single::Field>
    </label>
  {{else}}
    {{! only show Valid Datacenters for the default partition }}
    {{#if (eq (or @partition 'default') 'default')}}
      <div class='type-toggle'>
        <span>Valid datacenters</span>
        <label>
          <input
            type='checkbox'
            name='{{this.name}}[isScoped]'
            checked={{if (not this.isScoped) 'checked'}}
            onchange={{action 'change'}}
          />
          <span>All</span>
        </label>
      </div>
    {{/if}}
    {{#if this.isScoped}}
      <DataSource
        @src={{uri '/*/*/*/datacenters'}}
        @onchange={{action (mut this.datacenters) value='data'}}
      />

      <div class='checkbox-group' role='group'>
        {{#each this.datacenters as |dc|}}
          <label class='type-checkbox'>
            <input
              type='checkbox'
              name='{{this.name}}[Datacenters]'
              value={{dc.Name}}
              checked={{if (includes dc.Name this.item.Datacenters) 'checked'}}
              onchange={{action 'change'}}
            />
            <span>{{dc.Name}}</span>
          </label>
        {{/each}}
        {{#each this.item.Datacenters as |dc|}}
          {{#if (not (find-by 'Name' dc this.datacenters))}}
            <label class='type-checkbox'>
              <input
                type='checkbox'
                name='{{this.name}}[Datacenters]'
                value={{dc}}
                checked='checked'
                onchange={{action 'change'}}
              />
              <span>{{dc}}</span>
            </label>
          {{/if}}
        {{/each}}
      </div>

    {{/if}}
  {{/if}}
  {{#if (eq this.item.template '')}}
    <label class='type-text'>
      <span>Description (Optional)</span>
      <textarea
        name='{{this.name}}[Description]'
        value={{this.item.Description}}
        oninput={{action 'change'}}
      ></textarea>
    </label>
  {{/if}}
</fieldset>