/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import { structuralEquals } from '../../../../base/common/equals.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { derived, derivedOpts } from '../../../../base/common/observable.js';
import './placeholderText.css';
import { registerEditorContribution } from '../../../browser/editorExtensions.js';
import { observableCodeEditor } from '../../../browser/observableCodeEditor.js';
import { ghostTextForeground } from '../../../common/core/editorColorRegistry.js';
import { Range } from '../../../common/core/range.js';
import { InjectedTextCursorStops } from '../../../common/model.js';
import { localize } from '../../../../nls.js';
import { registerColor } from '../../../../platform/theme/common/colorUtils.js';
/**
 * Use the editor option to set the placeholder text.
*/
export class PlaceholderTextContribution extends Disposable {
    constructor(_editor) {
        super();
        this._editor = _editor;
        this._editorObs = observableCodeEditor(this._editor);
        this._placeholderText = this._editorObs.getOption(88 /* EditorOption.placeholder */);
        this._decorationOptions = derivedOpts({ owner: this, equalsFn: structuralEquals }, reader => {
            const p = this._placeholderText.read(reader);
            if (!p) {
                return undefined;
            }
            if (!this._editorObs.valueIsEmpty.read(reader)) {
                return undefined;
            }
            return { placeholder: p };
        });
        this._decorations = derived(this, (reader) => {
            const options = this._decorationOptions.read(reader);
            if (!options) {
                return [];
            }
            return [{
                    range: new Range(1, 1, 1, 1),
                    options: {
                        description: 'placeholder',
                        showIfCollapsed: true,
                        after: {
                            content: options.placeholder,
                            cursorStops: InjectedTextCursorStops.None,
                            inlineClassName: 'placeholder-text'
                        }
                    }
                }];
        });
        this._register(this._editorObs.setDecorations(this._decorations));
    }
}
PlaceholderTextContribution.ID = 'editor.contrib.placeholderText';
registerEditorContribution(PlaceholderTextContribution.ID, PlaceholderTextContribution, 0 /* EditorContributionInstantiation.Eager */);
registerColor('editor.placeholder.foreground', { dark: ghostTextForeground, light: ghostTextForeground, hcDark: ghostTextForeground, hcLight: ghostTextForeground }, localize('placeholderForeground', 'Foreground color of the placeholder text in the editor.'));
