import * as vscode from 'vscode' ; import { Sequence , isObservable , isThenable } from '../../utils/observable' ; export const NEXT_FN = "onNext();" ; export class Wizard { constructor ( private readonly w ) { } async showPage ( htmlBody ) { if ( isObservable ( htmlBody ) ) { const webview = this . w . webview ; htmlBody . subscribe ( { async onNext ( value ) { return await webview . postMessage ( { command : 'showPage' , html : value } ) ; } } ) ; } else if ( isThenable ( htmlBody ) ) { await this . w . webview . postMessage ( { command : 'showPage' , html : await htmlBody } ) ; } else { await this . w . webview . postMessage ( { command : 'showPage' , html : htmlBody } ) ; } this . w . reveal ( ) ; } } export interface Subscriber { onStep ( w , m ) : void ; onCancel ( ) : void ; } export function createWizard ( tabTitle , formId , s ) { const nextScript = `<script>
    const vscode = acquireVsCodeApi();
    const wvcontent = document.getElementById('wvcontent__');

    window.addEventListener('message', (e) => {
        const msg = e.data;
        switch (msg.command) {
            case 'showPage':
                wvcontent.innerHTML = msg.html;
                break;
        }
    });

    function onNext() {
        const s = { };
        for (const e of document.forms['${ formId }'].elements) {
            s[e.name] = e.value;
        }
        vscode.postMessage(s);
    }
    </script>` ; const html = `<html><body><div id='wvcontent__' />${ nextScript }</body></html>` ; const w = vscode . window . createWebviewPanel ( 'vsk8s-dialog' , tabTitle , vscode . ViewColumn . Active , { retainContextWhenHidden : true , enableScripts : true , } ) ; const wizard = new Wizard ( w ) ; w . webview . html = html ; w . onDidDispose ( ( ) => s . onCancel ( ) ) ; w . webview . onDidReceiveMessage ( ( m ) => { s . onStep ( wizard , m ) ; } ) ; w . reveal ( ) ; return wizard ; }