"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = codeWrapper;
var _react = _interopRequireDefault(require("react"));
var _Tab = require("@kui-shell/core/mdist/api/Tab");
var _filter = _interopRequireDefault(require("./filter"));
var _frontmatter = require("../../frontmatter");
var _Ansi = _interopRequireDefault(require("../../../Scalar/Ansi"));
var _Inputv = _interopRequireDefault(require("../../../../Views/Terminal/Block/Inputv2"));
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.
 */

const SimpleEditor = _react.default.lazy(() => Promise.resolve().then(() => require('../../../Editor/SimpleEditor')));
class Code extends _react.default.PureComponent {
  constructor() {
    super(...arguments);
    this._onResponse = (status, response) => {
      this.setState({
        response: {
          status,
          response,
          replayed: false
        }
      });
    };
  }
  render() {
    var _a;
    const {
      props
    } = this;
    const {
      mdprops,
      uuid,
      codeBlockResponses
    } = props;
    if (props.inline) {
      return _react.default.createElement("code", {
        className: props.className
      }, props.children);
    }
    const code = String(props.children).trim();
    const {
      body,
      attributes
    } = (0, _frontmatter.tryFrontmatter)(code);
    const tabUUID = mdprops.tab ? (0, _Tab.getPrimaryTabId)(mdprops.tab) : undefined;
    // react-markdown v6+ places the language in the className
    const match = /language-(\w+)/.exec(props.className || '');
    const language = match ? match[1] : undefined;
    if (language === 'ansi') {
      return _react.default.createElement(_Ansi.default, null, body);
    }
    if (mdprops.nested && props.codeIdx && mdprops.executableCodeBlocks !== false) {
      // onContentChange={body => this.splice(codeWithResponseFrontmatter(body, attributes.response), props.node.position.start.offset, props.node.position.end.offset)}
      const myCodeIdx = parseInt(props.codeIdx);
      const _response = ((_a = this.state) === null || _a === void 0 ? void 0 : _a.response) || codeBlockResponses(myCodeIdx);
      const status = _response ? _response.status : undefined;
      const response = _response ? _response.response : undefined;
      // i.e. executed in *this* session
      const executed = _response && !_response.replayed;
      // note how in the evaluation of `status`, we assume that if a
      // response is given but no status, that the status is 'done',
      // i.e. executed successfully to completion
      const statusConsideringReplay = !executed && (status === 'done' || status === 'error') ? 'replayed' : status;
      // don't show the input part, only the output part, of this code block
      // TODO: we should also look at the command registration, for the command that was executed
      const outputOnly = attributes.outputOnly === true || attributes.outputOnly === 'true';
      const blockId = attributes.id || `${uuid}-${myCodeIdx}`;
      return _react.default.createElement(_Inputv.default, {
        readonly: false,
        id: `kui-link-${blockId}`,
        className: "kui--code-block-in-markdown",
        tab: mdprops.tab,
        value: body,
        watch: attributes.watch,
        language: language,
        blockId: blockId,
        cleanup: attributes.cleanup,
        validate: attributes.validate,
        optional: attributes.optional,
        response: response,
        status: statusConsideringReplay,
        rawStatus: status,
        arg1: myCodeIdx,
        onResponse: this._onResponse,
        outputOnly: outputOnly,
        executeImmediately: mdprops.executeImmediately || attributes.execute === 'now',
        "data-code-index": myCodeIdx,
        "data-is-maximized": attributes.maximize
      });
    } else {
      const {
        bodyForView,
        languageForView
      } = (0, _filter.default)(body, language, attributes);
      return _react.default.createElement("div", {
        className: 'paragraph' + (mdprops.executableCodeBlocks === false ? ' kui--inverted-color-context' : '')
      }, !language ? _react.default.createElement("pre", null, _react.default.createElement("code", null, body)) : _react.default.createElement("code", {
        className: "kui--code--editor"
      }, _react.default.createElement(SimpleEditor, {
        tabUUID: tabUUID,
        content: bodyForView,
        contentType: languageForView,
        fontSize: 12,
        simple: true,
        minHeight: 0,
        readonly: true
      })));
    }
  }
}
function codeWrapper(mdprops, uuid, codeBlockResponses) {
  return function code(props) {
    return _react.default.createElement(Code, Object.assign({}, props, {
      mdprops: mdprops,
      uuid: uuid,
      codeBlockResponses: codeBlockResponses
    }));
  };
}