import { URI } from '../../../../base/common/uri.js';
import { OpenEditor } from '../common/files.js';
import { EditorResourceAccessor, SideBySideEditor } from '../../../common/editor.js';
import { List } from '../../../../base/browser/ui/list/listWidget.js';
import { ExplorerItem } from '../common/explorerModel.js';
import { coalesce } from '../../../../base/common/arrays.js';
import { AsyncDataTree } from '../../../../base/browser/ui/tree/asyncDataTree.js';
import { isActiveElement } from '../../../../base/browser/dom.js';
function getFocus(listService) {
    const list = listService.lastFocusedList;
    const element = list?.getHTMLElement();
    if (element && isActiveElement(element)) {
        let focus;
        if (list instanceof List) {
            const focused = list.getFocusedElements();
            if (focused.length) {
                focus = focused[0];
            }
        }
        else if (list instanceof AsyncDataTree) {
            const focused = list.getFocus();
            if (focused.length) {
                focus = focused[0];
            }
        }
        return focus;
    }
    return undefined;
}
function getResourceForCommand(resource, listService, editorService) {
    if (URI.isUri(resource)) {
        return resource;
    }
    const focus = getFocus(listService);
    if (focus instanceof ExplorerItem) {
        return focus.resource;
    }
    else if (focus instanceof OpenEditor) {
        return focus.getResource();
    }
    return EditorResourceAccessor.getOriginalUri(editorService.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY });
}
function getMultiSelectedResources(resource, listService, editorService, editorGroupService, explorerService) {
    const list = listService.lastFocusedList;
    const element = list?.getHTMLElement();
    if (element && isActiveElement(element)) {
        if (list instanceof AsyncDataTree && list.getFocus().every(item => item instanceof ExplorerItem)) {
            const context = explorerService.getContext(true, true);
            if (context.length) {
                return ( context.map(c => c.resource));
            }
        }
        if (list instanceof List) {
            const selection = coalesce(( list.getSelectedElements().filter(s => s instanceof OpenEditor).map((oe) => oe.getResource())));
            const focusedElements = list.getFocusedElements();
            const focus = focusedElements.length ? focusedElements[0] : undefined;
            let mainUriStr = undefined;
            if (URI.isUri(resource)) {
                mainUriStr = ( resource.toString());
            }
            else if (focus instanceof OpenEditor) {
                const focusedResource = focus.getResource();
                mainUriStr = focusedResource ? ( focusedResource.toString()) : undefined;
            }
            if (( selection.some(s => ( s.toString()) === mainUriStr))) {
                return selection;
            }
        }
    }
    const activeGroup = editorGroupService.activeGroup;
    const selection = activeGroup.selectedEditors;
    if (selection.length > 1 && URI.isUri(resource)) {
        if (( selection.some(e => e.matches({ resource })))) {
            return ( selection.map(editor => EditorResourceAccessor.getOriginalUri(editor))).filter(uri => !!uri);
        }
    }
    const result = getResourceForCommand(resource, listService, editorService);
    return !!result ? [result] : [];
}
function getOpenEditorsViewMultiSelection(listService, editorGroupService) {
    const list = listService.lastFocusedList;
    const element = list?.getHTMLElement();
    if (element && isActiveElement(element)) {
        if (list instanceof List) {
            const selection = coalesce(list.getSelectedElements().filter(s => s instanceof OpenEditor));
            const focusedElements = list.getFocusedElements();
            const focus = focusedElements.length ? focusedElements[0] : undefined;
            let mainEditor = undefined;
            if (focus instanceof OpenEditor) {
                mainEditor = focus;
            }
            if (( selection.some(s => s === mainEditor))) {
                return selection;
            }
            return mainEditor ? [mainEditor] : undefined;
        }
    }
    return undefined;
}
export { getMultiSelectedResources, getOpenEditorsViewMultiSelection, getResourceForCommand };
