import { PixelRatio } from '../../../../base/browser/pixelRatio.js';
import { getWindow, $ as $$1, append, addDisposableListener, EventType, getDomNodePagePosition } from '../../../../base/browser/dom.js';
import { GlobalPointerMoveMonitor } from '../../../../base/browser/globalPointerMoveMonitor.js';
import { Widget } from '../../../../base/browser/ui/widget.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { Color, HSVA, RGBA } from '../../../../base/common/color.js';
import { Emitter } from '../../../../base/common/event.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import './colorPicker.css.js';
import { localizeWithPath } from '../../../../nls.js';
import '../../../../platform/theme/common/colorUtils.js';
import '../../../../platform/theme/common/colors/baseColors.js';
import '../../../../platform/theme/common/colors/chartsColors.js';
import { editorHoverBackground } from '../../../../platform/theme/common/colors/editorColors.js';
import '../../../../platform/theme/common/colors/inputColors.js';
import '../../../../platform/theme/common/colors/listColors.js';
import '../../../../platform/theme/common/colors/menuColors.js';
import '../../../../platform/theme/common/colors/minimapColors.js';
import '../../../../platform/theme/common/colors/miscColors.js';
import '../../../../platform/theme/common/colors/quickpickColors.js';
import '../../../../platform/theme/common/colors/searchColors.js';
import { registerIcon } from '../../../../platform/theme/common/iconRegistry.js';
const _moduleId = "vs/editor/contrib/colorPicker/browser/colorPickerWidget";
const $ = $$1;
class ColorPickerHeader extends Disposable {
    constructor(container, model, themeService, showingStandaloneColorPicker = false) {
        super();
        this.model = model;
        this.showingStandaloneColorPicker = showingStandaloneColorPicker;
        this._closeButton = null;
        this._domNode = $('.colorpicker-header');
        append(container, this._domNode);
        this._pickedColorNode = append(this._domNode, $('.picked-color'));
        append(this._pickedColorNode, $('span.codicon.codicon-color-mode'));
        this._pickedColorPresentation = append(this._pickedColorNode, document.createElement('span'));
        this._pickedColorPresentation.classList.add('picked-color-presentation');
        const tooltip = ( localizeWithPath(_moduleId, 0, "Click to toggle color options (rgb/hsl/hex)"));
        this._pickedColorNode.setAttribute('title', tooltip);
        this._originalColorNode = append(this._domNode, $('.original-color'));
        this._originalColorNode.style.backgroundColor = Color.Format.CSS.format(this.model.originalColor) || '';
        this.backgroundColor = themeService.getColorTheme().getColor(editorHoverBackground) || Color.white;
        this._register(themeService.onDidColorThemeChange(theme => {
            this.backgroundColor = theme.getColor(editorHoverBackground) || Color.white;
        }));
        this._register(addDisposableListener(this._pickedColorNode, EventType.CLICK, () => this.model.selectNextColorPresentation()));
        this._register(addDisposableListener(this._originalColorNode, EventType.CLICK, () => {
            this.model.color = this.model.originalColor;
            this.model.flushColor();
        }));
        this._register(model.onDidChangeColor(this.onDidChangeColor, this));
        this._register(model.onDidChangePresentation(this.onDidChangePresentation, this));
        this._pickedColorNode.style.backgroundColor = Color.Format.CSS.format(model.color) || '';
        this._pickedColorNode.classList.toggle('light', model.color.rgba.a < 0.5 ? this.backgroundColor.isLighter() : model.color.isLighter());
        this.onDidChangeColor(this.model.color);
        if (this.showingStandaloneColorPicker) {
            this._domNode.classList.add('standalone-colorpicker');
            this._closeButton = this._register(( (new CloseButton(this._domNode))));
        }
    }
    get domNode() {
        return this._domNode;
    }
    get closeButton() {
        return this._closeButton;
    }
    get pickedColorNode() {
        return this._pickedColorNode;
    }
    get originalColorNode() {
        return this._originalColorNode;
    }
    onDidChangeColor(color) {
        this._pickedColorNode.style.backgroundColor = Color.Format.CSS.format(color) || '';
        this._pickedColorNode.classList.toggle('light', color.rgba.a < 0.5 ? this.backgroundColor.isLighter() : color.isLighter());
        this.onDidChangePresentation();
    }
    onDidChangePresentation() {
        this._pickedColorPresentation.textContent = this.model.presentation ? this.model.presentation.label : '';
    }
}
class CloseButton extends Disposable {
    constructor(container) {
        super();
        this._onClicked = this._register(( (new Emitter())));
        this.onClicked = this._onClicked.event;
        this._button = document.createElement('div');
        this._button.classList.add('close-button');
        append(container, this._button);
        const innerDiv = document.createElement('div');
        innerDiv.classList.add('close-button-inner-div');
        append(this._button, innerDiv);
        const closeButton = append(innerDiv, $('.button' + ThemeIcon.asCSSSelector(registerIcon('color-picker-close', Codicon.close, ( localizeWithPath(_moduleId, 1, 'Icon to close the color picker'))))));
        closeButton.classList.add('close-icon');
        this._register(addDisposableListener(this._button, EventType.CLICK, () => {
            this._onClicked.fire();
        }));
    }
}
class ColorPickerBody extends Disposable {
    constructor(container, model, pixelRatio, isStandaloneColorPicker = false) {
        super();
        this.model = model;
        this.pixelRatio = pixelRatio;
        this._insertButton = null;
        this._domNode = $('.colorpicker-body');
        append(container, this._domNode);
        this._saturationBox = ( (new SaturationBox(this._domNode, this.model, this.pixelRatio)));
        this._register(this._saturationBox);
        this._register(this._saturationBox.onDidChange(this.onDidSaturationValueChange, this));
        this._register(this._saturationBox.onColorFlushed(this.flushColor, this));
        this._opacityStrip = ( (new OpacityStrip(this._domNode, this.model, isStandaloneColorPicker)));
        this._register(this._opacityStrip);
        this._register(this._opacityStrip.onDidChange(this.onDidOpacityChange, this));
        this._register(this._opacityStrip.onColorFlushed(this.flushColor, this));
        this._hueStrip = ( (new HueStrip(this._domNode, this.model, isStandaloneColorPicker)));
        this._register(this._hueStrip);
        this._register(this._hueStrip.onDidChange(this.onDidHueChange, this));
        this._register(this._hueStrip.onColorFlushed(this.flushColor, this));
        if (isStandaloneColorPicker) {
            this._insertButton = this._register(( (new InsertButton(this._domNode))));
            this._domNode.classList.add('standalone-colorpicker');
        }
    }
    flushColor() {
        this.model.flushColor();
    }
    onDidSaturationValueChange({ s, v }) {
        const hsva = this.model.color.hsva;
        this.model.color = ( (new Color( (new HSVA(hsva.h, s, v, hsva.a)))));
    }
    onDidOpacityChange(a) {
        const hsva = this.model.color.hsva;
        this.model.color = ( (new Color( (new HSVA(hsva.h, hsva.s, hsva.v, a)))));
    }
    onDidHueChange(value) {
        const hsva = this.model.color.hsva;
        const h = (1 - value) * 360;
        this.model.color = ( (new Color( (new HSVA(h === 360 ? 0 : h, hsva.s, hsva.v, hsva.a)))));
    }
    get domNode() {
        return this._domNode;
    }
    get saturationBox() {
        return this._saturationBox;
    }
    get opacityStrip() {
        return this._opacityStrip;
    }
    get hueStrip() {
        return this._hueStrip;
    }
    get enterButton() {
        return this._insertButton;
    }
    layout() {
        this._saturationBox.layout();
        this._opacityStrip.layout();
        this._hueStrip.layout();
    }
}
class SaturationBox extends Disposable {
    constructor(container, model, pixelRatio) {
        super();
        this.model = model;
        this.pixelRatio = pixelRatio;
        this._onDidChange = ( (new Emitter()));
        this.onDidChange = this._onDidChange.event;
        this._onColorFlushed = ( (new Emitter()));
        this.onColorFlushed = this._onColorFlushed.event;
        this._domNode = $('.saturation-wrap');
        append(container, this._domNode);
        this._canvas = document.createElement('canvas');
        this._canvas.className = 'saturation-box';
        append(this._domNode, this._canvas);
        this.selection = $('.saturation-selection');
        append(this._domNode, this.selection);
        this.layout();
        this._register(addDisposableListener(this._domNode, EventType.POINTER_DOWN, e => this.onPointerDown(e)));
        this._register(this.model.onDidChangeColor(this.onDidChangeColor, this));
        this.monitor = null;
    }
    get domNode() {
        return this._domNode;
    }
    get canvas() {
        return this._canvas;
    }
    onPointerDown(e) {
        if (!e.target || !(e.target instanceof Element)) {
            return;
        }
        this.monitor = this._register(( (new GlobalPointerMoveMonitor())));
        const origin = getDomNodePagePosition(this._domNode);
        if (e.target !== this.selection) {
            this.onDidChangePosition(e.offsetX, e.offsetY);
        }
        this.monitor.startMonitoring(e.target, e.pointerId, e.buttons, event => this.onDidChangePosition(event.pageX - origin.left, event.pageY - origin.top), () => null);
        const pointerUpListener = addDisposableListener(e.target.ownerDocument, EventType.POINTER_UP, () => {
            this._onColorFlushed.fire();
            pointerUpListener.dispose();
            if (this.monitor) {
                this.monitor.stopMonitoring(true);
                this.monitor = null;
            }
        }, true);
    }
    onDidChangePosition(left, top) {
        const s = Math.max(0, Math.min(1, left / this.width));
        const v = Math.max(0, Math.min(1, 1 - (top / this.height)));
        this.paintSelection(s, v);
        this._onDidChange.fire({ s, v });
    }
    layout() {
        this.width = this._domNode.offsetWidth;
        this.height = this._domNode.offsetHeight;
        this._canvas.width = this.width * this.pixelRatio;
        this._canvas.height = this.height * this.pixelRatio;
        this.paint();
        const hsva = this.model.color.hsva;
        this.paintSelection(hsva.s, hsva.v);
    }
    paint() {
        const hsva = this.model.color.hsva;
        const saturatedColor = ( (new Color( (new HSVA(hsva.h, 1, 1, 1)))));
        const ctx = this._canvas.getContext('2d');
        const whiteGradient = ctx.createLinearGradient(0, 0, this._canvas.width, 0);
        whiteGradient.addColorStop(0, 'rgba(255, 255, 255, 1)');
        whiteGradient.addColorStop(0.5, 'rgba(255, 255, 255, 0.5)');
        whiteGradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
        const blackGradient = ctx.createLinearGradient(0, 0, 0, this._canvas.height);
        blackGradient.addColorStop(0, 'rgba(0, 0, 0, 0)');
        blackGradient.addColorStop(1, 'rgba(0, 0, 0, 1)');
        ctx.rect(0, 0, this._canvas.width, this._canvas.height);
        ctx.fillStyle = Color.Format.CSS.format(saturatedColor);
        ctx.fill();
        ctx.fillStyle = whiteGradient;
        ctx.fill();
        ctx.fillStyle = blackGradient;
        ctx.fill();
    }
    paintSelection(s, v) {
        this.selection.style.left = `${s * this.width}px`;
        this.selection.style.top = `${this.height - v * this.height}px`;
    }
    onDidChangeColor(color) {
        if (this.monitor && this.monitor.isMonitoring()) {
            return;
        }
        this.paint();
        const hsva = color.hsva;
        this.paintSelection(hsva.s, hsva.v);
    }
}
class Strip extends Disposable {
    constructor(container, model, showingStandaloneColorPicker = false) {
        super();
        this.model = model;
        this._onDidChange = ( (new Emitter()));
        this.onDidChange = this._onDidChange.event;
        this._onColorFlushed = ( (new Emitter()));
        this.onColorFlushed = this._onColorFlushed.event;
        if (showingStandaloneColorPicker) {
            this.domNode = append(container, $('.standalone-strip'));
            this.overlay = append(this.domNode, $('.standalone-overlay'));
        }
        else {
            this.domNode = append(container, $('.strip'));
            this.overlay = append(this.domNode, $('.overlay'));
        }
        this.slider = append(this.domNode, $('.slider'));
        this.slider.style.top = `0px`;
        this._register(addDisposableListener(this.domNode, EventType.POINTER_DOWN, e => this.onPointerDown(e)));
        this._register(model.onDidChangeColor(this.onDidChangeColor, this));
        this.layout();
    }
    layout() {
        this.height = this.domNode.offsetHeight - this.slider.offsetHeight;
        const value = this.getValue(this.model.color);
        this.updateSliderPosition(value);
    }
    onDidChangeColor(color) {
        const value = this.getValue(color);
        this.updateSliderPosition(value);
    }
    onPointerDown(e) {
        if (!e.target || !(e.target instanceof Element)) {
            return;
        }
        const monitor = this._register(( (new GlobalPointerMoveMonitor())));
        const origin = getDomNodePagePosition(this.domNode);
        this.domNode.classList.add('grabbing');
        if (e.target !== this.slider) {
            this.onDidChangeTop(e.offsetY);
        }
        monitor.startMonitoring(e.target, e.pointerId, e.buttons, event => this.onDidChangeTop(event.pageY - origin.top), () => null);
        const pointerUpListener = addDisposableListener(e.target.ownerDocument, EventType.POINTER_UP, () => {
            this._onColorFlushed.fire();
            pointerUpListener.dispose();
            monitor.stopMonitoring(true);
            this.domNode.classList.remove('grabbing');
        }, true);
    }
    onDidChangeTop(top) {
        const value = Math.max(0, Math.min(1, 1 - (top / this.height)));
        this.updateSliderPosition(value);
        this._onDidChange.fire(value);
    }
    updateSliderPosition(value) {
        this.slider.style.top = `${(1 - value) * this.height}px`;
    }
}
class OpacityStrip extends Strip {
    constructor(container, model, showingStandaloneColorPicker = false) {
        super(container, model, showingStandaloneColorPicker);
        this.domNode.classList.add('opacity-strip');
        this.onDidChangeColor(this.model.color);
    }
    onDidChangeColor(color) {
        super.onDidChangeColor(color);
        const { r, g, b } = color.rgba;
        const opaque = ( (new Color( (new RGBA(r, g, b, 1)))));
        const transparent = ( (new Color( (new RGBA(r, g, b, 0)))));
        this.overlay.style.background = `linear-gradient(to bottom, ${opaque} 0%, ${transparent} 100%)`;
    }
    getValue(color) {
        return color.hsva.a;
    }
}
class HueStrip extends Strip {
    constructor(container, model, showingStandaloneColorPicker = false) {
        super(container, model, showingStandaloneColorPicker);
        this.domNode.classList.add('hue-strip');
    }
    getValue(color) {
        return 1 - (color.hsva.h / 360);
    }
}
class InsertButton extends Disposable {
    constructor(container) {
        super();
        this._onClicked = this._register(( (new Emitter())));
        this.onClicked = this._onClicked.event;
        this._button = append(container, document.createElement('button'));
        this._button.classList.add('insert-button');
        this._button.textContent = 'Insert';
        this._register(addDisposableListener(this._button, EventType.CLICK, () => {
            this._onClicked.fire();
        }));
    }
    get button() {
        return this._button;
    }
}
class ColorPickerWidget extends Widget {
    static { this.ID = 'editor.contrib.colorPickerWidget'; }
    constructor(container, model, pixelRatio, themeService, standaloneColorPicker = false) {
        super();
        this.model = model;
        this.pixelRatio = pixelRatio;
        this._register(PixelRatio.getInstance(getWindow(container)).onDidChange(() => this.layout()));
        this._domNode = $('.colorpicker-widget');
        container.appendChild(this._domNode);
        this.header = this._register(( (new ColorPickerHeader(this._domNode, this.model, themeService, standaloneColorPicker))));
        this.body = this._register(( (new ColorPickerBody(this._domNode, this.model, this.pixelRatio, standaloneColorPicker))));
    }
    getId() {
        return ColorPickerWidget.ID;
    }
    layout() {
        this.body.layout();
    }
    get domNode() {
        return this._domNode;
    }
}
export { ColorPickerBody, ColorPickerHeader, ColorPickerWidget, InsertButton };
