
import { Codicon } from '../../../../../base/common/codicons.js';
import { basename } from '../../../../../base/common/resources.js';
import { ThemeIcon } from '../../../../../base/common/themables.js';
import { URI } from '../../../../../base/common/uri.js';
import { isLocation } from '../../../../../editor/common/languages.js';
import { localize } from '../../../../../nls.js';
import { encodeBase64, VSBuffer, decodeBase64 } from '../../../../../base/common/buffer.js';

var OmittedState;
(function(OmittedState) {
    OmittedState[OmittedState["NotOmitted"] = 0] = "NotOmitted";
    OmittedState[OmittedState["Partial"] = 1] = "Partial";
    OmittedState[OmittedState["Full"] = 2] = "Full";
})(OmittedState || (OmittedState = {}));
var IDiagnosticVariableEntryFilterData;
(function(IDiagnosticVariableEntryFilterData) {
    IDiagnosticVariableEntryFilterData.icon = Codicon.error;
    function fromMarker(marker) {
        return {
            filterUri: marker.resource,
            owner: marker.owner,
            problemMessage: marker.message,
            filterRange: {
                startLineNumber: marker.startLineNumber,
                endLineNumber: marker.endLineNumber,
                startColumn: marker.startColumn,
                endColumn: marker.endColumn
            }
        };
    }
    IDiagnosticVariableEntryFilterData.fromMarker = fromMarker;
    function toEntry(data) {
        return {
            id: id(data),
            name: label(data),
            icon: IDiagnosticVariableEntryFilterData.icon,
            value: data,
            kind: "diagnostic",
            ...data
        };
    }
    IDiagnosticVariableEntryFilterData.toEntry = toEntry;
    function id(data) {
        return [
            data.filterUri,
            data.owner,
            data.filterSeverity,
            data.filterRange?.startLineNumber,
            data.filterRange?.startColumn
        ].join(":");
    }
    IDiagnosticVariableEntryFilterData.id = id;
    function label(data) {
        let TrimThreshold;
        (function(TrimThreshold) {
            TrimThreshold[TrimThreshold["MaxChars"] = 30] = "MaxChars";
            TrimThreshold[TrimThreshold["MaxSpaceLookback"] = 10] = "MaxSpaceLookback";
        })(TrimThreshold || (TrimThreshold = {}));
        if (data.problemMessage) {
            if (data.problemMessage.length < 30 ) {
                return data.problemMessage;
            }
            const lastSpace = data.problemMessage.lastIndexOf(" ", 30 );
            if (lastSpace === -1 || lastSpace + 10  < 30 ) {
                return data.problemMessage.substring(0, 30 ) + "…";
            }
            return data.problemMessage.substring(0, lastSpace) + "…";
        }
        let labelStr = ( localize(7190, "All Problems"));
        if (data.filterUri) {
            labelStr = ( localize(7191, "Problems in {0}", basename(data.filterUri)));
        }
        return labelStr;
    }
    IDiagnosticVariableEntryFilterData.label = label;
})(
    IDiagnosticVariableEntryFilterData || (IDiagnosticVariableEntryFilterData = {})
);
var IChatRequestVariableEntry;
(function(IChatRequestVariableEntry) {
    function toUri(entry) {
        return URI.isUri(entry.value) ? entry.value : isLocation(entry.value) ? entry.value.uri : undefined;
    }
    IChatRequestVariableEntry.toUri = toUri;
    function toExport(v) {
        if (v.value instanceof Uint8Array) {
            const dup = {
                ...v
            };
            dup.value = {
                $base64: encodeBase64(VSBuffer.wrap(v.value))
            };
            return dup;
        }
        return v;
    }
    IChatRequestVariableEntry.toExport = toExport;
    function fromExport(v) {
        if (v && "values" in v && Array.isArray(v.values)) {
            return {
                kind: "generic",
                id: v.id ?? "",
                name: v.name,
                value: v.values[0]?.value,
                range: v.range,
                modelDescription: v.modelDescription,
                references: v.references
            };
        } else {
            if (v.value && typeof v.value === "object" && "$base64" in v.value && typeof v.value.$base64 === "string") {
                const dup = {
                    ...v
                };
                dup.value = decodeBase64(v.value.$base64).buffer;
                return dup;
            }
            return v;
        }
    }
    IChatRequestVariableEntry.fromExport = fromExport;
})(IChatRequestVariableEntry || (IChatRequestVariableEntry = {}));
function isImplicitVariableEntry(obj) {
    return obj.kind === "implicit";
}
function isStringVariableEntry(obj) {
    return obj.kind === "string";
}
function isTerminalVariableEntry(obj) {
    return obj.kind === "terminalCommand";
}
function isPasteVariableEntry(obj) {
    return obj.kind === "paste";
}
function isWorkspaceVariableEntry(obj) {
    return obj.kind === "workspace";
}
function isImageVariableEntry(obj) {
    return obj.kind === "image";
}
function isNotebookOutputVariableEntry(obj) {
    return obj.kind === "notebookOutput";
}
function isElementVariableEntry(obj) {
    return obj.kind === "element";
}
function isPromptFileVariableEntry(obj) {
    return obj.kind === "promptFile";
}
function isPromptTextVariableEntry(obj) {
    return obj.kind === "promptText";
}
function isSCMHistoryItemVariableEntry(obj) {
    return obj.kind === "scmHistoryItem";
}
function isSCMHistoryItemChangeVariableEntry(obj) {
    return obj.kind === "scmHistoryItemChange";
}
function isSCMHistoryItemChangeRangeVariableEntry(obj) {
    return obj.kind === "scmHistoryItemChangeRange";
}
function isStringImplicitContextValue(value) {
    const asStringImplicitContextValue = value;
    return (typeof asStringImplicitContextValue === "object" && asStringImplicitContextValue !== null && (typeof asStringImplicitContextValue.value === "string" || typeof asStringImplicitContextValue.value === "undefined") && (typeof asStringImplicitContextValue.name === "string" || typeof asStringImplicitContextValue.name === "undefined") && (asStringImplicitContextValue.resourceUri === undefined || URI.isUri(asStringImplicitContextValue.resourceUri)) && (typeof asStringImplicitContextValue.name === "string" || URI.isUri(asStringImplicitContextValue.resourceUri)) && (asStringImplicitContextValue.icon === undefined || ThemeIcon.isThemeIcon(asStringImplicitContextValue.icon)) && URI.isUri(asStringImplicitContextValue.uri) && typeof asStringImplicitContextValue.handle === "number");
}
var PromptFileVariableKind;
(function(PromptFileVariableKind) {
    PromptFileVariableKind["Instruction"] = "vscode.instructions.file.root";
    PromptFileVariableKind["InstructionReference"] = "vscode.instructions.file.reference";
    PromptFileVariableKind["PromptFile"] = "vscode.prompt.file";
})(PromptFileVariableKind || (PromptFileVariableKind = {}));
function toPromptFileVariableEntry(uri, kind, originLabel, automaticallyAdded = false, toolReferences) {
    return {
        id: `${kind}__${( uri.toString())}`,
        name: `prompt:${basename(uri)}`,
        value: uri,
        kind: "promptFile",
        modelDescription: "Prompt instructions file",
        isRoot: kind !== PromptFileVariableKind.InstructionReference,
        originLabel,
        toolReferences,
        automaticallyAdded
    };
}
var PromptTextVariableKind;
(function(PromptTextVariableKind) {
    PromptTextVariableKind["CustomizationsIndex"] = "vscode.customizations.index";
})(PromptTextVariableKind || (PromptTextVariableKind = {}));
function toPromptTextVariableEntry(content, automaticallyAdded = false, toolReferences) {
    return {
        id: PromptTextVariableKind.CustomizationsIndex,
        name: `prompt:customizationsIndex`,
        value: content,
        kind: "promptText",
        modelDescription: "Chat customizations index",
        automaticallyAdded,
        toolReferences
    };
}
function toToolVariableEntry(entry, range) {
    return {
        kind: "tool",
        id: entry.id,
        icon: ThemeIcon.isThemeIcon(entry.icon) ? entry.icon : undefined,
        name: entry.displayName,
        value: undefined,
        range
    };
}
function toToolSetVariableEntry(entry, range) {
    return {
        kind: "toolset",
        id: entry.id,
        icon: entry.icon,
        name: entry.referenceName,
        value: ( Array.from(entry.getTools()).map(t => toToolVariableEntry(t))),
        range
    };
}
class ChatRequestVariableSet {
    constructor(entries) {
        this._ids = ( new Set());
        this._entries = [];
        if (entries) {
            this.add(...entries);
        }
    }
    add(...entry) {
        for (const e of entry) {
            if (!( this._ids.has(e.id))) {
                this._ids.add(e.id);
                this._entries.push(e);
            }
        }
    }
    insertFirst(entry) {
        if (!( this._ids.has(entry.id))) {
            this._ids.add(entry.id);
            this._entries.unshift(entry);
        }
    }
    remove(entry) {
        this._ids.delete(entry.id);
        this._entries = this._entries.filter(e => e.id !== entry.id);
    }
    has(entry) {
        return ( this._ids.has(entry.id));
    }
    asArray() {
        return this._entries.slice(0);
    }
    get length() {
        return this._entries.length;
    }
}

export { ChatRequestVariableSet, IChatRequestVariableEntry, IDiagnosticVariableEntryFilterData, OmittedState, PromptFileVariableKind, isElementVariableEntry, isImageVariableEntry, isImplicitVariableEntry, isNotebookOutputVariableEntry, isPasteVariableEntry, isPromptFileVariableEntry, isPromptTextVariableEntry, isSCMHistoryItemChangeRangeVariableEntry, isSCMHistoryItemChangeVariableEntry, isSCMHistoryItemVariableEntry, isStringImplicitContextValue, isStringVariableEntry, isTerminalVariableEntry, isWorkspaceVariableEntry, toPromptFileVariableEntry, toPromptTextVariableEntry, toToolSetVariableEntry, toToolVariableEntry };
