"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _core = require("@kui-shell/core");
var _Input = _interopRequireDefault(require("./Input"));
var _Output = _interopRequireDefault(require("./Output"));
var _BlockModel = require("./BlockModel");
var _MutabilityContext = require("../../../Client/MutabilityContext");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
 * Copyright 2020 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.
 */

class Block extends _react.default.PureComponent {
  constructor(props) {
    super(props);
    this._willChangeSize = this.willChangeSize.bind(this);
    this._onOutputRender = this.onOutputRender.bind(this);
    this.state = {
      isFocused: false,
      isMaximized: false
    };
  }
  /** Owner wants us to focus on the current prompt */
  doFocus() {
    if (this._input) {
      this._input.doFocus();
    }
  }
  inputValue() {
    return this._input && this._input.value();
  }
  /** Child wants to maximize/restore */
  willChangeSize(width) {
    this.setState({
      isMaximized: width === "100%" /* Width.Maximized */
    });

    setTimeout(() => {
      _core.Events.eventBus.emitTabLayoutChange(this.props.tab.uuid);
      if (this.state._block) {
        this.state._block.scrollIntoView(true);
      }
    });
  }
  onOutputRender() {
    if (this.props.onOutputRender) {
      this.props.onOutputRender();
    }
    // oof: this is for bottom input clients... but it messes up
    // notebooks, constantly scrolling to bottom...
    /* if (this.props.noActiveInput && this.state._block) {
      this.state._block.scrollIntoView()
    } */
  }

  output() {
    if ((0, _BlockModel.isActive)(this.props.model) || (0, _BlockModel.isFinished)(this.props.model) || (0, _BlockModel.isProcessing)(this.props.model)) {
      return _react.default.createElement(_Output.default, {
        key: (0, _BlockModel.isEmpty)(this.props.model) ? undefined : this.props.model.execUUID,
        uuid: this.props.uuid,
        tab: this.props.tab,
        idx: this.props.idx,
        model: this.props.model,
        splitPosition: this.props.splitPosition,
        willRemove: this.props.willRemove,
        willChangeSize: this._willChangeSize,
        onRender: this._onOutputRender,
        willUpdateCommand: this.props.willUpdateCommand,
        isWidthConstrained: this.props.isWidthConstrained,
        willFocusBlock: this.props.willFocusBlock
      });
    }
  }
  customInput() {
    if (this.props.children && _react.default.isValidElement(this.props.children)) {
      return _react.default.cloneElement(this.props.children, {
        idx: this.props.idx,
        tab: this.props.tab,
        uuid: this.props.uuid,
        block: this.state._block
      });
    }
  }
  input() {
    return this.customInput() || this.state._block && _react.default.createElement(_Input.default, Object.assign({
      key: this.props.uuid,
      uuid: this.props.uuid,
      tab: this.props.tab,
      model: this.props.model,
      isExperimental: this.props.isExperimental
    }, this.props, {
      willFocusBlock: this.props.willFocusBlock,
      _block: this.state._block,
      ref: c => this._input = c
    }), this.props.children);
  }
  /**
   * For Active or Empty blocks, just show the <Input/>, otherwise
   * wrap the <Input/>-<Output/> pair.
   *
   */
  render() {
    const hideOut = (0, _BlockModel.hideOutput)(this.props.model);
    return (!this.props.noActiveInput || !(0, _BlockModel.isActive)(this.props.model)) && _react.default.createElement(_MutabilityContext.MutabilityContext.Consumer, null, value => _react.default.createElement("li", {
      className: 'repl-block ' + (hideOut ? '' : this.props.model.state.toString()),
      "data-is-executable": value.executable || undefined,
      "data-is-editable": value.editable || undefined,
      "data-is-maximized": this.state.isMaximized || (0, _BlockModel.isMaximized)(this.props.model) || undefined,
      "data-is-output-only": (0, _BlockModel.isOutputOnly)(this.props.model) || undefined,
      "data-is-empty": (0, _BlockModel.isEmpty)(this.props.model) || undefined,
      "data-announcement": (0, _BlockModel.isAnnouncement)(this.props.model) || undefined,
      "data-uuid": (0, _BlockModel.hasUUID)(this.props.model) && this.props.model.execUUID,
      "data-scrollback-uuid": this.props.uuid,
      "data-input-count": this.props.idx,
      "data-is-focused": this.props.isFocused || undefined,
      "data-is-replay": (0, _BlockModel.isReplay)(this.props.model) || undefined,
      ref: c => this.setState({
        _block: c
      }),
      tabIndex: (0, _BlockModel.isActive)(this.props.model) ? -1 : 1,
      onClick: this.props.willFocusBlock,
      onFocus: this.props.onFocus
    }, _react.default.createElement(_react.default.Fragment, null, (0, _BlockModel.isAnnouncement)(this.props.model) || (0, _BlockModel.isOutputOnly)(this.props.model) ? this.output() : /* isActive(this.props.model) || */(0, _BlockModel.isEmpty)(this.props.model) ? this.input() : _react.default.createElement(_react.default.Fragment, null, this.input(), !hideOut && this.output()))));
  }
}
exports.default = Block;