


import { LocationDto, DefinitionLinkDto, WorkspaceSymbolDto, LinkDto, CodeActionDto, reviveWorkspaceEditDto } from '@workbench-stack/extension-api';
import * as modes from '@vscode/monaco-editor/esm/vs/editor/common/modes';
import { SuggestResultDto } from '@workbench-stack/extension-api/lib/protocol';
import { URI } from '@vscode/monaco-editor/esm/vs/base/common/uri';
import * as search from '@finder-stack/core';


//#region --- revive functions

export function _reviveLocationDto(data: LocationDto | LocationDto[]): modes.Location | modes.Location[] {
    if (!data) {
        return <modes.Location>data;
    } else if (Array.isArray(data)) {
        data.forEach(l => _reviveLocationDto(l));
        return <modes.Location[]>data;
    } else {
        data.uri = URI.revive(data.uri);
        return <modes.Location>data;
    }
}

export function _reviveLocationLinkDto(data: DefinitionLinkDto | DefinitionLinkDto[]): modes.LocationLink | modes.LocationLink[] {
    if (!data) {
        return <modes.LocationLink>data;
    } else if (Array.isArray(data)) {
        data.forEach(l => _reviveLocationLinkDto(l));
        return <modes.LocationLink[]>data;
    } else {
        data.uri = URI.revive(data.uri);
        return <modes.LocationLink>data;
    }
}


export function _reviveWorkspaceSymbolDto(data: WorkspaceSymbolDto | WorkspaceSymbolDto[] | undefined): search.IWorkspaceSymbol | search.IWorkspaceSymbol[] | undefined {
    if (!data) {
        return <undefined>data;
    } else if (Array.isArray(data)) {
        data.forEach(_reviveWorkspaceSymbolDto);
        return /*<search.IWorkspaceSymbol[]>*/data as any;
    } else {
        data.location = <LocationDto>_reviveLocationDto(data.location);
        return /*<search.IWorkspaceSymbol>*/data as any;
    }
}


export function _reviveCodeActionDto(data: CodeActionDto[] | undefined): modes.CodeAction[] {
    if (data) {
        data.forEach(code => reviveWorkspaceEditDto(code.edit));
    }
    return <modes.CodeAction[]>data;
}

export function _reviveLinkDTO(data: LinkDto): modes.ILink {
    if (data.url && typeof data.url !== 'string') {
        data.url = URI.revive(data.url);
    }
    return <modes.ILink>data;
}

export function toCompletionSuggestion(result: SuggestResultDto) {
    if (!result) {
        return result;
    }
    return {
        suggestions: result.suggestions,
        incomplete: result.incomplete,
    };
}
