/*
 * 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 { Parser } from 'madwizard';
import SplitInjector from '../../../Views/Terminal/SplitInjector';
import _tabbed, { isTabs } from './tabbed';
import { isNormalSplit } from '../KuiFrontmatter';
const Wizard = React.lazy(() => import('./Wizard'));
const ReactCommentary = React.lazy(() => import('../../Commentary').then(_ => ({ default: _.ReactCommentary })));
export default function divWrapper(mdprops, uuid, choices) {
    const tabbed = _tabbed(uuid, choices);
    return function div(props) {
        const maximized = props['data-kui-maximized'] === 'true';
        const position = props['data-kui-split'];
        const placeholder = props['data-kui-placeholder'];
        const count = props['data-kui-split-count'] ? parseInt(props['data-kui-split-count'], 10) : undefined;
        // Important! Default to `undefined` so that we pick up the
        // *default* behavior from the Terminal component
        const hasActiveInput = position === 'terminal' || undefined;
        const inverseColors = props['data-kui-inverse-colors'] === 'true' || undefined;
        if (isTabs(props)) {
            return tabbed(props);
        }
        else if (!position ||
            ((isNormalSplit(position) || position === 'wizard') && count === 0 && !maximized && !placeholder)) {
            // don't create a split if a position wasn't indicated, or if
            // this is the first default-positioned section; if it is
            // maximized, we'll have to go through the injector path
            const node = Parser.isWizard(props) ? (React.createElement(Wizard, Object.assign({ uuid: uuid }, props, { choices: choices }))) : (React.createElement("div", { "data-is-maximized": maximized || undefined }, props.children));
            if (!maximized && (!mdprops.tab || (hasActiveInput !== true && hasActiveInput !== false))) {
                return node;
            }
            else {
                // modify the current split to have the desired properties
                return (React.createElement(SplitInjector.Consumer, null, injector => injector.modify(mdprops.tab.uuid, node, { hasActiveInput, inverseColors, maximized })));
            }
        }
        else {
            // then we have a section that targets a given split position
            return (React.createElement(SplitInjector.Consumer, null, injector => {
                const node = (React.createElement(ReactCommentary, null,
                    React.createElement("div", { className: "padding-content marked-content page-content", "data-is-nested": true }, Parser.isWizard(props) ? (React.createElement(Wizard, Object.assign({ uuid: uuid }, props, { choices: choices }))) : (props.children || (placeholder ? React.createElement("span", { className: "italic sub-text" }, placeholder) : '')))));
                const positionForView = position === 'terminal' || position === 'wizard' ? 'default' : position;
                setTimeout(() => injector.inject([
                    {
                        uuid,
                        node,
                        position: (positionForView + (isNormalSplit(positionForView) ? '' : '-strip')),
                        count,
                        opts: { inverseColors, hasActiveInput, maximized }
                    }
                ]));
                return React.createElement(React.Fragment, null);
            }));
        }
    };
}
//# sourceMappingURL=div.js.map