/*
 * Copyright 2022 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 = (this && this.__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());
    });
};
import React from 'react';
import { v4 } from 'uuid';
import { basename } from 'path';
import { Util, encodeComponent, isPopup } from '@kui-shell/core';
import fetchMarkdownFile from './fetch';
import { setTabReadonly } from './commentary'; // TODO move to core?
import { tryFrontmatter } from '../components/Content/Markdown/frontmatter-parser';
function response(filepath, { data, codeBlockResponses }) {
    return {
        apiVersion: 'kui-shell/v1',
        kind: 'CommentaryResponse',
        props: {
            filepath,
            children: data,
            codeBlockResponses
        }
    };
}
function show(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const filepath = args.argvNoOptions[1];
        if (!args.parsedOptions.here) {
            yield args.REPL.qexec(`tab new --cmdline "show --here ${encodeComponent(filepath)}"`);
            return true;
        }
        setTabReadonly(args);
        return response(filepath, yield fetchMarkdownFile(filepath, args));
    });
}
function guide(args) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const filepath = args.argvNoOptions[1];
            if (!args.parsedOptions.here && !isPopup()) {
                yield args.REPL.qexec(`tab new --cmdline "guide --here ${encodeComponent(filepath)}"`);
                return true;
            }
            const reader = (file) => __awaiter(this, void 0, void 0, function* () {
                const { data } = yield fetchMarkdownFile(file.path, args);
                file.value = data;
                return file;
            });
            const { data: rawData /* , codeBlockResponses */ } = yield fetchMarkdownFile(filepath, args);
            const { attributes } = tryFrontmatter(rawData);
            const { VFile } = yield import('vfile');
            const { Choices, Parser } = yield import('madwizard');
            const input = new VFile({
                value: rawData,
                cwd: Util.cwd(),
                path: Util.expandHomeDir(filepath)
            });
            const choices = Choices.newChoiceState('default');
            const { blocks } = yield Parser.blockify(input, reader, choices);
            const { default: Guide } = yield import('../components/Content/Markdown/components/guide');
            const { default: Imports } = yield import('../components/Content/Markdown/components/Imports');
            const { default: SplitInjector } = yield import('../components/Views/Terminal/SplitInjector');
            const title = attributes.title || basename(filepath);
            const { execUUID: uuid } = args.execOptions;
            const tree = React.createElement(Imports, { imports: blocks, choices, title });
            const guide = React.createElement(Guide, { tab: args.tab, uuid, blocks, choices, title });
            const react = React.createElement(SplitInjector.Consumer, undefined, injector => {
                injector.inject([{ uuid: v4(), node: tree, position: 'left-strip', count: 0, opts: { inverseColors: true } }]);
                return injector.modify(args.tab.uuid, guide, { maximized: true });
            });
            args.tab.setTitle(title);
            setTabReadonly(args);
            return { react };
        }
        catch (err) {
            console.error(err);
            throw err;
        }
        /* const data = `---
      title: ${attributes.title || basename(filepath)}
      layout:
          1: left
          2:
              position: default
              maximized: true
          - ${Util.expandHomeDir(filepath)}
      ---
      
      ::imports
      
      ---
      
      ::guide
      `
      
        setTabReadonly(args)
        return response(filepath, {
          data,
          codeBlockResponses
        }) */
    });
}
/**
 * This plugin introduces the commentary command
 *
 */
export default function registerGuideController(commandTree) {
    const flags = {
        boolean: ['here']
    };
    commandTree.listen('/show', show, { outputOnly: true, flags });
    commandTree.listen('/guide', guide, { outputOnly: true, flags });
}
//# sourceMappingURL=guide.js.map