// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import { assert } from 'chai';
import * as SDK from '../../core/sdk/sdk.js';
import { assertScreenshot, dispatchKeyDownEvent, renderElementIntoDOM } from '../../testing/DOMHelpers.js';
import { describeWithEnvironment } from '../../testing/EnvironmentHelpers.js';
import * as Sources from './sources.js';
function setCodeMirrorContent(editor, content) {
    editor.dispatch({
        changes: { from: 0, to: editor.state.doc.length, insert: content },
    });
}
async function getDialogAndEditor(editorLineNumber, oldCondition, isLogpoint, onFinish) {
    const dialog = new Sources.BreakpointEditDialog.BreakpointEditDialog();
    dialog.editorLineNumber = editorLineNumber;
    dialog.oldCondition = oldCondition;
    dialog.breakpointType =
        isLogpoint ? "LOGPOINT" /* SDK.DebuggerModel.BreakpointType.LOGPOINT */ : "CONDITIONAL_BREAKPOINT" /* SDK.DebuggerModel.BreakpointType.CONDITIONAL_BREAKPOINT */;
    dialog.onFinish = onFinish;
    await dialog.updateComplete;
    const textEditor = dialog.contentElement.querySelector('devtools-text-editor');
    assert.exists(textEditor);
    return { dialog, editor: textEditor.editor };
}
function setBreakpointType(dialog, newType) {
    const selectElement = dialog.contentElement.querySelector('devtools-toolbar select');
    selectElement.value = newType;
    selectElement.dispatchEvent(new Event('change'));
}
// Note that we currently don't install a fake RuntimeModel + ExecutionContext for these tests.
// This means the 'BreakpointEditDialog' won't be able to check whether the
// condition is a complete JavaScript expression or not and simply assume it is.
describeWithEnvironment('BreakpointEditDialog', function () {
    it('reports a committed condition when the Enter key is pressed', async () => {
        const resultPromise = new Promise(async (resolve, reject) => {
            try {
                const { editor, dialog } = await getDialogAndEditor(0, '', false, resolve);
                setCodeMirrorContent(editor, 'x === 5');
                renderElementIntoDOM(dialog, { includeCommonStyles: true });
                await assertScreenshot('sources/breakpoint-edit-dialog.png');
                dispatchKeyDownEvent(editor.contentDOM, { key: 'Enter' });
            }
            catch (e) {
                reject(e);
            }
        });
        const { committed, condition } = await resultPromise;
        assert.isTrue(committed);
        assert.strictEqual(condition, 'x === 5');
    });
    it('does not report a commited condition when the ESC key is pressed', async () => {
        const resultPromise = new Promise(async (resolve) => {
            const { editor } = await getDialogAndEditor(0, '', false, resolve);
            setCodeMirrorContent(editor, 'hello');
            dispatchKeyDownEvent(editor.contentDOM, { key: 'Escape' });
        });
        const { committed } = await resultPromise;
        assert.isFalse(committed);
    });
    it('commits condition when close button is clicked', async () => {
        const resultPromise = new Promise(async (resolve) => {
            const { dialog, editor } = await getDialogAndEditor(0, '', false, resolve);
            setCodeMirrorContent(editor, 'x === 5');
            dialog.contentElement.querySelector('.dialog-header > devtools-icon').click();
        });
        const { committed, condition } = await resultPromise;
        assert.isTrue(committed);
        assert.strictEqual(condition, 'x === 5');
    });
    it('leaves the condition as-is for logpoints', async () => {
        const resultPromise = new Promise(async (resolve) => {
            const { editor } = await getDialogAndEditor(0, '', true, resolve);
            setCodeMirrorContent(editor, 'x');
            dispatchKeyDownEvent(editor.contentDOM, { key: 'Enter' });
        });
        const { condition } = await resultPromise;
        assert.strictEqual(condition, 'x');
    });
    it('result includes isLogpoint for logpoints', async () => {
        const resultPromise = new Promise(async (resolve) => {
            const { editor } = await getDialogAndEditor(0, '', true, resolve);
            setCodeMirrorContent(editor, 'x');
            dispatchKeyDownEvent(editor.contentDOM, { key: 'Enter' });
        });
        const { isLogpoint } = await resultPromise;
        assert.isTrue(isLogpoint);
    });
    it('result includes isLogpoint for conditional breakpoints', async () => {
        const resultPromise = new Promise(async (resolve) => {
            const { editor } = await getDialogAndEditor(0, '', false, resolve);
            setCodeMirrorContent(editor, 'x === 5');
            dispatchKeyDownEvent(editor.contentDOM, { key: 'Enter' });
        });
        const { isLogpoint } = await resultPromise;
        assert.isFalse(isLogpoint);
    });
    it('prefills the input with the old condition', async () => {
        const { editor } = await getDialogAndEditor(0, 'x === 42', false, () => { });
        assert.strictEqual(editor.state.doc.sliceString(0), 'x === 42');
    });
    // Flaky
    it.skip('[crbug.com/505352053] focuses the editor input field after changing the breakpoint type', async () => {
        const { dialog, editor } = await getDialogAndEditor(0, '', false, () => { });
        renderElementIntoDOM(dialog.contentElement);
        setBreakpointType(dialog, "LOGPOINT" /* SDK.DebuggerModel.BreakpointType.LOGPOINT */);
        await dialog.updateComplete;
        assert.isTrue(editor.hasFocus);
    });
    // Flaky
    it.skip('[crbug.com/505352053] focuses the editor when focus() is called, even if it is not yet rendered', async () => {
        const { dialog, editor } = await getDialogAndEditor(0, '', false, () => { });
        renderElementIntoDOM(dialog.contentElement);
        assert.isFalse(editor.hasFocus);
        // Trigger an update to test the focus() method waiting for it.
        dialog.editorLineNumber = 1;
        dialog.focus();
        await dialog.updateComplete;
        assert.isTrue(editor.hasFocus);
    });
});
//# sourceMappingURL=BreakpointEditDialog.test.js.map