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 { on } from '@ember/modifier';
import { action } from '@ember/object';
import dateMath from '@elastic/datemath';
import pick from 'ember-composable-helpers/helpers/pick';
import set from 'ember-set-helper/helpers/set';
import { lte } from 'ember-truth-helpers';
import moment from 'moment';
import randomId from '../../../-private/random-id.js';
import EuiButton from '../../eui-button.js';
import EuiButtonIcon from '../../eui-button-icon.js';
import EuiFieldNumber from '../../eui-field-number.js';
import EuiFlexGroup from '../../eui-flex-group.js';
import EuiFlexItem from '../../eui-flex-item.js';
import EuiI18nComponent from '../../eui-i18n.js';
import EuiSelect from '../../eui-select.js';
import EuiSpacer from '../../eui-spacer.js';
import EuiToolTip from '../../eui-tool-tip.js';
import { parseTimeParts } from '../utils/quick-select.js';
import { NEXT } from '../utils/time-options.js';

var _class, _descriptor, _descriptor2, _descriptor3;
let EuiQuickSelect = setComponentTemplate(precompileTemplate(`
    {{#let (randomId) as |legendId|}}
      <fieldset>
        <EuiFlexGroup
          @responsive={{false}}
          @alignItems="center"
          @justifyContent="spaceBetween"
          @gutterSize="s"
        >
          <EuiFlexItem @grow={{false}}>
            <EuiI18n
              @token="euiQuickSelect.quickSelectTitle"
              @default="Quick select"
              as |Token|
            >
              <Token as |quickSelectTitle|>
                <div aria-hidden class="euiFormLabel">
                  {{quickSelectTitle}}
                </div>
              </Token>
            </EuiI18n>
          </EuiFlexItem>
          <EuiFlexItem @grow={{false}}>
            <EuiFlexGroup
              @alignItems="center"
              @gutterSize="s"
              @responsive={{false}}
            >
              <EuiFlexItem @grow={{false}}>
                <EuiI18n
                  @token="euiQuickSelect.previousLabel"
                  @default="Previous time window"
                  as |Token|
                >
                  <Token as |previousLabel|>
                    <EuiToolTip @content={{previousLabel}}>
                      <EuiButtonIcon
                        aria-label={{previousLabel}}
                        @iconType="arrowLeft"
                        {{on "click" this.stepBackward}}
                      />
                    </EuiToolTip>
                  </Token>
                </EuiI18n>
              </EuiFlexItem>
              <EuiFlexItem @grow={{false}}>
                <EuiI18n
                  @token="euiQuickSelect.nextLabel"
                  @default="Next time window"
                  as |Token|
                >
                  <Token as |nextLabel|>
                    <EuiToolTip @content={{nextLabel}}>
                      <EuiButtonIcon
                        aria-label={{nextLabel}}
                        @iconType="arrowRight"
                        {{on "click" this.stepForward}}
                      />
                    </EuiToolTip>
                  </Token>
                </EuiI18n>
              </EuiFlexItem>
            </EuiFlexGroup>
          </EuiFlexItem>
        </EuiFlexGroup>
        <EuiSpacer @size="s" />
        <EuiFlexGroup @gutterSize="s" @responsive={{false}}>
          <EuiFlexItem>
            <EuiI18n
              @token="euiQuickSelect.tenseLabel"
              @default="Time tense"
              as |Token|
            >
              <Token as |tenseLabel|>
                <EuiSelect
                  @compressed={{true}}
                  aria-label={{tenseLabel}}
                  @value={{this.timeTense}}
                  @options={{@timeOptions.timeTenseOptions}}
                  {{on "change" (pick "target.value" (set this "timeTense"))}}
                />
              </Token>
            </EuiI18n>
          </EuiFlexItem>
          <EuiFlexItem>
            <EuiI18n
              @token="euiQuickSelect.valueLabel"
              @default="Time value"
              as |Token|
            >
              <Token as |valueLabel|>
                <EuiFieldNumber
                  @compressed={{true}}
                  aria-label={{valueLabel}}
                  @value={{this.timeValue}}
                  {{on "input" (pick "target.value" (set this "timeValue"))}}
                />
              </Token>
            </EuiI18n>
          </EuiFlexItem>
          <EuiFlexItem>
            <EuiI18n
              @token="euiQuickSelect.unitLabel"
              @default="Time unit"
              as |Token|
            >
              <Token as |unitLabel|>
                <EuiSelect
                  @compressed={{true}}
                  aria-label={{unitLabel}}
                  @value={{this.timeUnits}}
                  @options={{@timeOptions.timeUnitsOptions}}
                  {{on "change" (pick "target.value" (set this "timeUnits"))}}
                />
              </Token>
            </EuiI18n>
          </EuiFlexItem>
          <EuiFlexItem @grow={{false}}>
            <EuiButton
              class="euiQuickSelect__applyButton"
              @size="s"
              @disabled={{lte this.timeValue 0}}
              {{on "click" this.applyQuickSelect}}
            >
              <EuiI18n
                @token="euiQuickSelect.applyButton"
                @default="Apply"
                as |Token|
              >
                <Token as |value|>
                  {{value}}
                </Token>
              </EuiI18n>
            </EuiButton>
          </EuiFlexItem>
        </EuiFlexGroup>
      </fieldset>
    {{/let}}
  `, {
  strictMode: true,
  scope: () => ({
    randomId,
    EuiFlexGroup,
    EuiFlexItem,
    EuiI18n: EuiI18nComponent,
    EuiToolTip,
    EuiButtonIcon,
    on,
    EuiSpacer,
    EuiSelect,
    pick,
    set,
    EuiFieldNumber,
    EuiButton,
    lte
  })
}), (_class = class EuiQuickSelect extends Component {
  constructor(owner, args) {
    super(owner, args);
    _initializerDefineProperty(this, "timeTense", _descriptor, this);
    _initializerDefineProperty(this, "timeValue", _descriptor2, this);
    _initializerDefineProperty(this, "timeUnits", _descriptor3, this);
    const {
      timeTense: timeTenseDefault,
      timeUnits: timeUnitsDefault,
      timeValue: timeValueDefault
    } = parseTimeParts(this.args.start, this.args.end);
    this.timeTense = this.args.prevQuickSelect && this.args.prevQuickSelect.timeTense ? this.args.prevQuickSelect.timeTense : timeTenseDefault;
    this.timeValue = this.args.prevQuickSelect && this.args.prevQuickSelect.timeValue ? this.args.prevQuickSelect.timeValue : timeValueDefault;
    this.timeUnits = this.args.prevQuickSelect && this.args.prevQuickSelect.timeUnits ? this.args.prevQuickSelect.timeUnits : timeUnitsDefault;
  }
  getBounds() {
    const startMoment = dateMath.parse(this.args.start);
    const endMoment = dateMath.parse(this.args.end, {
      roundUp: true
    });
    return {
      min: startMoment && startMoment.isValid() ? startMoment : moment().subtract(15, 'minute'),
      max: endMoment && endMoment.isValid() ? endMoment : moment()
    };
  }
  applyQuickSelect() {
    if (this.timeTense === NEXT) {
      this.args.applyTime({
        start: 'now',
        end: `now+${this.timeValue}${this.timeUnits}`
        // quickSelect: { ...this.state },
      });
    } else {
      this.args.applyTime({
        start: `now-${this.timeValue}${this.timeUnits}`,
        end: 'now'
        // quickSelect: { ...this.state },
      });
    }
  }
  stepForward() {
    const {
      min,
      max
    } = this.getBounds();
    const diff = max.diff(min);
    this.args.applyTime({
      start: moment(max).add(1, 'ms').toISOString(),
      end: moment(max).add(diff + 1, 'ms').toISOString(),
      keepPopoverOpen: true
    });
  }
  stepBackward() {
    const {
      min,
      max
    } = this.getBounds();
    const diff = max.diff(min);
    this.args.applyTime({
      start: moment(min).subtract(diff + 1, 'ms').toISOString(),
      end: moment(min).subtract(1, 'ms').toISOString(),
      keepPopoverOpen: true
    });
  }
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "timeTense", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "timeValue", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "timeUnits", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _applyDecoratedDescriptor(_class.prototype, "applyQuickSelect", [action], Object.getOwnPropertyDescriptor(_class.prototype, "applyQuickSelect"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "stepForward", [action], Object.getOwnPropertyDescriptor(_class.prototype, "stepForward"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "stepBackward", [action], Object.getOwnPropertyDescriptor(_class.prototype, "stepBackward"), _class.prototype)), _class));

export { EuiQuickSelect as default };
//# sourceMappingURL=eui-quick-select.js.map
