"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _Divider = require("@patternfly/react-core/dist/esm/components/Divider");
var _Select = require("@patternfly/react-core/dist/esm/components/Select");
var _Util = require("@kui-shell/core/mdist/api/Util");
var _Exec = require("@kui-shell/core/mdist/api/Exec");
var _model = require("../model");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
 * Copyright 2021 The Kubernetes Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function (resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function (resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
class PatternFlySelect extends _react.default.PureComponent {
  constructor(props) {
    super(props);
    this._onClicks = (0, _model.isGrouped)(this.props) ? (0, _Util.flatten)(this.props.groups.map(group => (0, _model.isDivider)(group) ? [] : group.options.map(option => this.onClick.bind(this, option)))) : this.props.options.map(option => this.onClick.bind(this, option));
    this._onSelect = this.onSelect.bind(this);
    this._onToggle = this.onToggle.bind(this);
    this.state = {
      isOpen: props.isOpen,
      selected: props.selected
    };
  }
  onToggle(expanded) {
    if (this.props.isClosable !== false) {
      if (expanded) {
        if (!this.state.isOpen) {
          this.setState({
            isOpen: true
          });
        }
      } else {
        this.setState({
          isOpen: false
        });
      }
    }
  }
  onSelect(_, value) {
    this.setState(curState => ({
      isOpen: this.props.isClosable !== false ? false : curState.isOpen,
      selected: typeof value === 'string' ? value : undefined
    }));
  }
  onClick(option) {
    return __awaiter(this, void 0, void 0, function* () {
      if (option.command) {
        if (typeof option.command === 'string') {
          (0, _Exec.pexecInCurrentTab)(option.command, undefined, undefined, option.quietExec);
        } else {
          (0, _Exec.pexecInCurrentTab)(yield option.command(), undefined, undefined, option.quietExec);
        }
      }
    });
  }
  /** @return UI for the given option */
  option(option, index) {
    return _react.default.createElement(_Select.SelectOption, {
      className: "kui--select-option",
      "data-value": option.label,
      key: index,
      value: option.label,
      isSelected: option.isSelected,
      description: option.description,
      onClick: this._onClicks[index],
      isDisabled: option.isDisabled
    });
  }
  /** @return UI for all of the options */
  options() {
    if ((0, _model.isGrouped)(this.props)) {
      let runningIdx = 0;
      const groups = this.props.groups.map((group, idx1) => (0, _model.isDivider)(group) ? _react.default.createElement(_Divider.Divider, {
        key: `divider-${idx1}`
      }) : _react.default.createElement(_Select.SelectGroup, {
        label: group.label,
        key: `group-${idx1}`
      }, group.options.map(option => this.option(option, runningIdx++))));
      return groups;
    } else {
      return this.props.options.map((option, idx) => this.option(option, idx));
    }
  }
  render() {
    return _react.default.createElement(_Select.Select, {
      className: 'kui--select' + (this.props.className ? ' ' + this.props.className : ''),
      isOpen: this.state.isOpen,
      variant: this.props.variant,
      typeAheadAriaLabel: "Select from the Options",
      selections: this.state.selected,
      isGrouped: (0, _model.isGrouped)(this.props),
      maxHeight: this.props.maxHeight,
      onToggle: this._onToggle,
      onSelect: this._onSelect,
      isDisabled: this.props.isDisabled
    }, this.options());
  }
}
exports.default = PatternFlySelect;