// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '/lens/region_selection.js';
import '/lens/post_selection_renderer.js';
import '/lens/overlay_border_glow.js';
import '/lens/overlay_shimmer_canvas.js';
import '/strings.m.js';
import '//resources/cr_elements/cr_button/cr_button.js';
import '//resources/cr_elements/cr_icon_button/cr_icon_button.js';
import '//resources/cr_elements/cr_toast/cr_toast.js';
import { assert } from '//resources/js/assert.js';
import { SelectionOverlayBaseLitElement } from '/lens/selection_overlay_base_lit.js';
import { DragFeature, GestureState } from '/lens/selection_utils.js';
import { getCss } from './glic_selection_overlay.css.js';
import { getHtml } from './glic_selection_overlay.html.js';
const GLIC_BORDER_GLOW_COLORS = [
    '#1B6EF3',
    '#0B57D0',
    '#1B6EF3',
    '#7CACF8',
];
export class SelectionOverlayElementElement extends SelectionOverlayBaseLitElement {
    static get is() {
        return 'glic-selection-overlay';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            ...super.properties,
            screenshotDataUri: { type: String },
            enableRegionSelectedGlow: { type: Boolean },
            enableBorderGlow: { type: Boolean },
            disableShimmer: { type: Boolean },
        };
    }
    #screenshotDataUri_accessor_storage = '';
    get screenshotDataUri() { return this.#screenshotDataUri_accessor_storage; }
    set screenshotDataUri(value) { this.#screenshotDataUri_accessor_storage = value; }
    #enableRegionSelectedGlow_accessor_storage = true;
    get enableRegionSelectedGlow() { return this.#enableRegionSelectedGlow_accessor_storage; }
    set enableRegionSelectedGlow(value) { this.#enableRegionSelectedGlow_accessor_storage = value; }
    #enableBorderGlow_accessor_storage = true;
    get enableBorderGlow() { return this.#enableBorderGlow_accessor_storage; }
    set enableBorderGlow(value) { this.#enableBorderGlow_accessor_storage = value; }
    #disableShimmer_accessor_storage = false;
    get disableShimmer() { return this.#disableShimmer_accessor_storage; }
    set disableShimmer(value) { this.#disableShimmer_accessor_storage = value; }
    constructor() {
        super();
    }
    get selectionElements() {
        return {
            backgroundImageCanvas: this.$.backgroundImageCanvas,
            cursor: this.$.cursor,
            initialFlashScrim: this.$.initialFlashScrim,
            overlayShimmerCanvas: this.$.overlayShimmerCanvas,
            postSelectionRenderer: this.$.postSelectionRenderer,
            regionSelectionLayer: this.$.regionSelectionLayer,
            selectionOverlay: this.$.selectionOverlay,
        };
    }
    updated(changedProperties) {
        super.updated(changedProperties);
    }
    firstUpdated() {
        super.firstUpdated();
        GLIC_BORDER_GLOW_COLORS.forEach((color, index) => {
            this.style.setProperty(`--overlay-border-glow-color-${index + 1}`, color);
        });
        this.updateThemeColors();
        this.resetCursor();
    }
    updateThemeColors() {
        const selectionColor = this.getSelectionElementColor();
        if (selectionColor) {
            this.style.setProperty('--color-selection-element', selectionColor);
        }
        const primaryColor = this.getPrimaryColor();
        if (primaryColor) {
            this.style.setProperty('--color-primary', primaryColor);
        }
    }
    get defaultCursorIconUrl() {
        return 'url("/glic_region_selection_cursor_icon.svg")';
    }
    handleGestureStart() {
        super.handleGestureStart();
        if (this.selectionElements.postSelectionRenderer.handleGestureStart(this.currentGesture)) {
            this.draggingRespondent = DragFeature.POST_SELECTION;
        }
    }
    handleGestureDrag(event) {
        assert(this.currentGesture.state === GestureState.DRAGGING);
        // Capture pointer events so gestures still work if the users pointer
        // leaves the selection overlay div. Pointer capture is implicitly
        // released after pointerup or pointercancel events.
        this.setPointerCapture(event.pointerId);
        if (this.draggingRespondent === DragFeature.POST_SELECTION) {
            this.selectionElements.postSelectionRenderer.handleGestureDrag(this.currentGesture);
            return;
        }
        // If no one is responding to the drag yet, then let the region selection
        // layer respond.
        if (this.draggingRespondent === DragFeature.NONE) {
            this.setCursorToCrosshair();
            this.draggingRespondent = DragFeature.MANUAL_REGION;
            this.activeRegionId = '';
            this.baseHandler.activeRegionId = '';
            this.selectionElements.postSelectionRenderer.clearSelection();
            // TODO(crbug.com/421002691): follow the convention where the layer
            // should return true if its handling the gesture, and draggingRespondent
            // should be updated. Currently used to trigger the fade in of the
            // darkened scrim.
            this.selectionElements.regionSelectionLayer.handleGestureStart();
        }
        if (this.draggingRespondent === DragFeature.MANUAL_REGION) {
            this.selectionElements.regionSelectionLayer.handleGestureDrag(this.currentGesture);
        }
    }
    handleGestureEnd() {
        // Allow proper feature to respond to the tap/drag event.
        switch (this.currentGesture.state) {
            case GestureState.DRAGGING:
                // Drag has finished. Let the features respond to the end of a drag.
                if (this.draggingRespondent === DragFeature.MANUAL_REGION) {
                    this.selectionElements.regionSelectionLayer.handleGestureEnd(this.currentGesture);
                }
                else if (this.draggingRespondent === DragFeature.POST_SELECTION) {
                    this.selectionElements.postSelectionRenderer.handleGestureEnd();
                    // Fade out scrim which is currently being managed by region selection
                    // TODO(crbug.com/420998632): move scrim out to its own component
                    this.selectionElements.regionSelectionLayer
                        .handlePostSelectionDragGestureEnd();
                }
                break;
            case GestureState.STARTING:
                if (this.draggingRespondent === DragFeature.NONE) {
                    this.baseHandler.activeRegionId = '';
                    this.selectionElements.regionSelectionLayer.handleGestureEnd(this.currentGesture);
                }
                break;
            default: // Other states are invalid and ignored.
                break;
        }
        this.resetCursor();
    }
    getOverlayBorderGlow() {
        return this.shadowRoot.querySelector('#overlayBorderGlow');
    }
}
customElements.define(SelectionOverlayElementElement.is, SelectionOverlayElementElement);
