import { _ as _applyDecoratedDescriptor, a as _initializerDefineProperty } from '../_rollupPluginBabelHelpers-9wjJaosZ.js';
import { setComponentTemplate } from '@ember/component';
import { precompileTemplate } from '@ember/template-compilation';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { fn } from '@ember/helper';
import { on } from '@ember/modifier';
import { action } from '@ember/object';
import didInsert from '@ember/render-modifiers/modifiers/did-insert';
import didUpdate from '@ember/render-modifiers/modifiers/did-update';
import { and, not, or } from 'ember-truth-helpers';
import randomId from '../-private/random-id.js';
import argOrDefault, { argOrDefaultDecorator } from '../helpers/arg-or-default.js';
import classNames from '../helpers/class-names.js';
import validatableControl from '../modifiers/validatable-control.js';
import { Browser } from '../utils/browser/browser.js';
import { keys } from '../utils/keys.js';
import EuiFormControlLayout from './eui-form-control-layout.js';

var _dec, _class, _descriptor, _descriptor2, _descriptor3;
let isSearchSupported = false;

/**
 * You can find more typing on the template
 */
let EuiFieldSearch = setComponentTemplate(precompileTemplate(`
    {{#let
      (and (not (argOrDefault @isFakePrependBlock false)) (has-block "prepend"))
      (and (not (argOrDefault @isFakeAppendBlock false)) (has-block "append"))
      (argOrDefault @id (randomId))
      as |hasPrepend hasAppend inputId|
    }}
      {{#let
        (classNames
          (if @fullWidth "euiFieldSearch--fullWidth")
          (if @compressed "euiFieldSearch--compressed")
          (if (or hasPrepend hasAppend) "euiFieldSearch--inGroup")
          (if @isLoading "euiFieldSearch--isLoading")
          (if
            (and (argOrDefault @isClearable true) @value)
            "euiFieldSearch--isClearable"
          )
          "euiFieldSearch"
        )
        as |classes|
      }}
        <EuiFormControlLayout
          @icon="search"
          @fullWidth={{@fullWidth}}
          @isLoading={{@isLoading}}
          @disabled={{@disabled}}
          @clear={{if
            (and
              @isClearable
              @value
              (not @readOnly)
              (and (not @isDisabled) (not @disabled))
            )
            this.onClear
          }}
          @compressed={{@compressed}}
          @useGroup={{or hasPrepend hasAppend}}
        >
          <:prepend as |prependClasses|>
            {{yield prependClasses inputId to="prepend"}}
          </:prepend>
          <:field>
            <input
              id={{inputId}}
              class={{classes}}
              value={{this.value}}
              disabled={{@disabled}}
              type="text"
              placeholder={{@placeholder}}
              ...attributes
              {{on "keyup" (fn this.onKeyUp this.incremental @onSearch)}}
              {{on "search" this.setValue}}
              {{on "change" this.setValue}}
              {{didInsert this.didInsertInput}}
              {{didUpdate this.didUpdateValue @value}}
              {{validatableControl @isInvalid}}
            />
          </:field>
          <:append as |appendClasses|>
            {{yield appendClasses inputId to="append"}}
          </:append>
        </EuiFormControlLayout>
      {{/let}}
    {{/let}}
  `, {
  strictMode: true,
  scope: () => ({
    and,
    not,
    argOrDefault,
    randomId,
    classNames,
    or,
    EuiFormControlLayout,
    on,
    fn,
    didInsert,
    didUpdate,
    validatableControl
  })
}), (_dec = argOrDefaultDecorator(false), (_class = class EuiFieldSearch extends Component {
  constructor(...args) {
    super(...args);
    _initializerDefineProperty(this, "inputElement", _descriptor, this);
    _initializerDefineProperty(this, "value", _descriptor2, this);
    _initializerDefineProperty(this, "incremental", _descriptor3, this);
  }
  setValue(e) {
    const value = e.target.value;
    if (this.value !== value) {
      this.value = value;
    }
    this.args.onSearch?.(this.value);
  }
  onKeyUp(incremental, onSearch, e) {
    this.value = e.target.value;
    if (this.args.onKeyUp) {
      this.args.onKeyUp(e);
      if (e.defaultPrevented) {
        return;
      }
    }
    if (onSearch && (e.key !== keys.ENTER && incremental || e.key === keys.ENTER && !isSearchSupported)) {
      onSearch(e.target.value);
    }
  }
  didUpdateValue() {
    this.value = this.args.value;
  }
  onClear() {
    const nativeInputValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
    const nativeInputValueSetter = nativeInputValue ? nativeInputValue.set : undefined;
    if (nativeInputValueSetter) {
      nativeInputValueSetter.call(this.inputElement, '');
    }

    // dispatch input event, with IE11 support/fallback
    let event;
    if ('Event' in window && typeof Event === 'function') {
      event = new Event('input', {
        bubbles: true,
        cancelable: false
      });
    } else {
      // IE11
      event = document.createEvent('Event');
      event.initEvent('input', true, false);
    }
    if (this.inputElement) {
      if (event) {
        this.inputElement.dispatchEvent(event);
      }

      // set focus on the search field
      this.inputElement.focus();
      this.inputElement.dispatchEvent(new Event('change'));
    }
    this.value = '';
    if (this.args.onSearch && this.incremental) {
      this.args.onSearch('');
    }
  }
  didInsertInput(inputRef) {
    this.inputElement = inputRef;
    isSearchSupported = Browser.isEventSupported('search', this.inputElement);
  }
  willDestroy() {
    super.willDestroy();
    this.inputElement = null;
  }
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "inputElement", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return null;
  }
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "value", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return this.args.value || (this.args.defaultValue ? `${this.args.defaultValue}` : '');
  }
}), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "incremental", [_dec], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return false;
  }
}), _applyDecoratedDescriptor(_class.prototype, "setValue", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setValue"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onKeyUp", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onKeyUp"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "didUpdateValue", [action], Object.getOwnPropertyDescriptor(_class.prototype, "didUpdateValue"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onClear", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onClear"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "didInsertInput", [action], Object.getOwnPropertyDescriptor(_class.prototype, "didInsertInput"), _class.prototype)), _class)));

export { EuiFieldSearch as default };
//# sourceMappingURL=eui-field-search.js.map
