/*
 * 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.
 */
import React from 'react';
import { getPrimaryTabId } from '@kui-shell/core';
import bodyAndLanguage from './filter';
import { tryFrontmatter } from '../../frontmatter';
import Ansi from '../../../Scalar/Ansi';
import CodeBlock from '../../../../Views/Terminal/Block/Inputv2';
const SimpleEditor = React.lazy(() => import('../../../Editor/SimpleEditor'));
export default function codeWrapper(mdprops, uuid, codeBlockResponses, spliceInCodeExecution) {
    return function code(props) {
        if (props.inline) {
            return React.createElement("code", { className: props.className }, props.children);
        }
        const code = String(props.children).trim();
        const { body, attributes } = tryFrontmatter(code);
        const tabUUID = mdprops.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.createElement(Ansi, 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 = 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.createElement(React.Fragment, null,
                React.createElement(CodeBlock, { 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: spliceInCodeExecution, outputOnly: outputOnly, executeImmediately: mdprops.executeImmediately || attributes.execute === 'now', "data-code-index": myCodeIdx, "data-is-maximized": attributes.maximize })));
        }
        else {
            const { bodyForView, languageForView } = bodyAndLanguage(body, language, attributes);
            return (React.createElement("div", { className: 'paragraph' + (mdprops.executableCodeBlocks === false ? ' kui--inverted-color-context' : '') }, !language ? (React.createElement("pre", null,
                React.createElement("code", null, body))) : (React.createElement("code", { className: "kui--code--editor" },
                React.createElement(SimpleEditor, { tabUUID: tabUUID, content: bodyForView, contentType: languageForView, fontSize: 12, simple: true, minHeight: 0, readonly: true })))));
        }
    };
}
//# sourceMappingURL=index.js.map