import templateOnly from '@ember/component/template-only';
import { setComponentTemplate } from '@ember/component';
import { precompileTemplate } from '@ember/template-compilation';
import { helper } from '@ember/component/helper';
import { array, fn } from '@ember/helper';
import { on } from '@ember/modifier';
import { and, eq, not, or, gt } from 'ember-truth-helpers';
import isArray from 'ember-truth-helpers/helpers/is-array';
import randomId from '../-private/random-id.js';
import argOrDefault from '../helpers/arg-or-default.js';
import classNames from '../helpers/class-names.js';
import useState from '../helpers/use-state.js';
import EuiFormErrorText from './eui-form-error-text.js';
import EuiFormHelpText from './eui-form-help-text.js';
import EuiFormLabel from './eui-form-label.js';

function startsWith([needle, word]) {
  return word?.startsWith(needle) || false;
}
const startWith = helper(startsWith);
const EuiFormRow = setComponentTemplate(precompileTemplate(`
  {{#let
    (classNames
      "euiFormRow"
      (if @hasEmptyLabelSpace "euiFormRow--hasEmptyLabelSpace")
      (if @fullWidth "euiFormRow--fullWidth")
      componentName="EuiFormRow"
      display=@display
    )
    (classNames
      "euiFormRow__fieldWrapper"
      (if (startWith "center" @display) "euiFormRow__fieldWrapperDisplayOnly")
    )
    (if (isArray @error) @error (array @error))
    (and @label (eq @labelType "legend"))
    (useState false)
    (argOrDefault @id (randomId))
    (argOrDefault @hasChildLabel true)
    (and (not (argOrDefault @isFakeLabelBlock false)) (has-block "label"))
    (and (not (argOrDefault @isFakeHelpTextBlock false)) (has-block "helpText"))
    as |classes fieldWrapperClasses errors isLegend focusedState rowId hasChildLabel hasLabelBlock hasHelpTextBlock|
  }}
    {{#if (eq @legendType "legend")}}
      <fieldset class={{classes}} id="{{rowId}}-row" ...attributes>
        {{#if (or @label @labelAppend hasLabelBlock)}}
          <div class="euiFormRow__labelWrapper">
            {{#if isLegend}}
              {{#if hasLabelBlock}}
                <EuiFormLabel
                  class="euiFormRow__label"
                  for={{rowId}}
                  aria-invalid={{@isInvalid}}
                  @isInvalid={{@isInvalid}}
                  @type={{@labelType}}
                >
                  {{yield @label to="label"}}
                </EuiFormLabel>
              {{else}}
                <EuiFormLabel
                  class="euiFormRow__label"
                  for={{rowId}}
                  aria-invalid={{@isInvalid}}
                  @isInvalid={{@isInvalid}}
                  @type={{@labelType}}
                >
                  {{@label}}
                </EuiFormLabel>
              {{/if}}
            {{else if hasLabelBlock}}
              <EuiFormLabel
                class="euiFormRow__label"
                aria-invalid={{@isInvalid}}
                for={{if hasChildLabel rowId}}
                @isInvalid={{@isInvalid}}
                @type={{@labelType}}
                @isFocused={{and
                  (not @isDisabled)
                  (not isLegend)
                  focusedState.value
                }}
              >
                {{yield @label to="label"}}
              </EuiFormLabel>
            {{else}}
              <EuiFormLabel
                class="euiFormRow__label"
                aria-invalid={{@isInvalid}}
                for={{if hasChildLabel rowId}}
                @isInvalid={{@isInvalid}}
                @type={{@labelType}}
                @isFocused={{and
                  (not @isDisabled)
                  (not isLegend)
                  focusedState.value
                }}
              >
                {{@label}}
              </EuiFormLabel>
            {{/if}}
          </div>
        {{/if}}
        <div
          class={{fieldWrapperClasses}}
          {{on "focusin" (fn focusedState.setState true)}}
          {{on "focusout" (fn focusedState.setState false)}}
        >
          {{#if (has-block "field")}}
            {{yield to="field"}}
          {{else}}
            {{yield}}
          {{/if}}
          {{#if
            (or
              (and (has-block "errors") @isInvalid)
              (and (gt errors.length 0) @isInvalid)
            )
          }}
            {{#if (has-block "errors")}}
              {{#each errors as |error i|}}
                <EuiFormErrorText
                  class="euiFormRow__text {{@errorClasses}}"
                  id="{{rowId}}-error-{{i}}"
                >
                  {{!@glint-ignore}}
                  {{yield error to="errors"}}
                </EuiFormErrorText>
              {{/each}}
            {{else}}
              {{#each errors as |error i|}}
                <EuiFormErrorText
                  class="euiFormRow__text {{@errorClasses}}"
                  id="{{rowId}}-error-{{i}}"
                >
                  {{!@glint-ignore}}
                  {{error}}
                </EuiFormErrorText>
              {{/each}}
            {{/if}}
          {{/if}}
          {{#if (or @helpText hasHelpTextBlock)}}
            {{#if hasHelpTextBlock}}
              <EuiFormHelpText id="{{rowId}}-help" class={{@helpTextClasses}}>
                {{yield @helpText to="helpText"}}
              </EuiFormHelpText>
            {{else}}
              <EuiFormHelpText id="{{rowId}}-help" class={{@helpTextClasses}}>
                {{@helpText}}
              </EuiFormHelpText>
            {{/if}}
          {{/if}}
        </div>
      </fieldset>
    {{else}}
      <div class={{classes}} id="{{rowId}}-row" ...attributes>
        {{#if (or @label @labelAppend hasLabelBlock)}}
          <div class="euiFormRow__labelWrapper">
            {{#if isLegend}}
              {{#if hasLabelBlock}}
                <EuiFormLabel
                  class="euiFormRow__label"
                  for={{rowId}}
                  aria-invalid={{@isInvalid}}
                  @isInvalid={{@isInvalid}}
                  @type={{@labelType}}
                >
                  {{yield @label to="label"}}
                </EuiFormLabel>
              {{else}}
                <EuiFormLabel
                  class="euiFormRow__label"
                  for={{rowId}}
                  aria-invalid={{@isInvalid}}
                  @isInvalid={{@isInvalid}}
                  @type={{@labelType}}
                >
                  {{@label}}
                </EuiFormLabel>
              {{/if}}
            {{else if hasLabelBlock}}
              <EuiFormLabel
                class="euiFormRow__label"
                aria-invalid={{@isInvalid}}
                for={{if hasChildLabel rowId}}
                @isInvalid={{@isInvalid}}
                @type={{@labelType}}
                @isFocused={{and
                  (not @isDisabled)
                  (not isLegend)
                  focusedState.value
                }}
              >
                {{yield @label to="label"}}
              </EuiFormLabel>
            {{else}}
              <EuiFormLabel
                class="euiFormRow__label"
                aria-invalid={{@isInvalid}}
                for={{if hasChildLabel rowId}}
                @isInvalid={{@isInvalid}}
                @type={{@labelType}}
                @isFocused={{and
                  (not @isDisabled)
                  (not isLegend)
                  focusedState.value
                }}
              >
                {{@label}}
              </EuiFormLabel>
            {{/if}}
          </div>
        {{/if}}
        <div
          class={{fieldWrapperClasses}}
          {{on "focusin" (fn focusedState.setState true)}}
          {{on "focusout" (fn focusedState.setState false)}}
        >
          {{#if (has-block "field")}}
            {{yield to="field"}}
          {{else}}
            {{yield}}
          {{/if}}
          {{#if
            (or
              (and (has-block "errors") @isInvalid)
              (and (gt errors.length 0) @isInvalid)
            )
          }}
            {{#if (has-block "errors")}}
              {{#each errors as |error i|}}
                <EuiFormErrorText
                  class="euiFormRow__text {{@errorClasses}}"
                  id="{{rowId}}-error-{{i}}"
                >
                  {{!@glint-ignore}}
                  {{yield error to="errors"}}
                </EuiFormErrorText>
              {{/each}}
            {{else}}
              {{#each errors as |error i|}}
                <EuiFormErrorText
                  class="euiFormRow__text {{@errorClasses}}"
                  id="{{rowId}}-error-{{i}}"
                >
                  {{!@glint-ignore}}
                  {{error}}
                </EuiFormErrorText>
              {{/each}}
            {{/if}}
          {{/if}}
          {{#if (or @helpText (has-block "helpText"))}}
            {{#if (has-block "helpText")}}
              <EuiFormHelpText id="{{rowId}}-help" class={{@helpTextClasses}}>
                {{yield to="helpText"}}
              </EuiFormHelpText>
            {{else}}
              {{#let
                (if (isArray @helpText) @helpText (array @helpText))
                as |helpTexts|
              }}
                {{#each helpTexts as |helpText i|}}
                  <EuiFormHelpText
                    id="{{rowId}}-help-{{i}}"
                    class={{@helpTextClasses}}
                  >
                    {{helpText}}
                  </EuiFormHelpText>
                {{/each}}
              {{/let}}
            {{/if}}
          {{/if}}
        </div>
      </div>
    {{/if}}
  {{/let}}
`, {
  strictMode: true,
  scope: () => ({
    classNames,
    startWith,
    isArray,
    array,
    and,
    eq,
    useState,
    argOrDefault,
    randomId,
    not,
    or,
    EuiFormLabel,
    on,
    fn,
    gt,
    EuiFormErrorText,
    EuiFormHelpText
  })
}), templateOnly("eui-form-row", "EuiFormRow"));

export { EuiFormRow as default, startsWith };
//# sourceMappingURL=eui-form-row.js.map
