
import { getDurationString, fromNow } from '../../../../../base/common/date.js';
import { isNumber } from '../../../../../base/common/types.js';
import { localize } from '../../../../../nls.js';
import { TerminalSettingId } from '../../../../../platform/terminal/common/terminal.js';
import { terminalDecorationIncomplete, terminalDecorationSuccess, terminalDecorationError } from '../terminalIcons.js';

var DecorationStyles;
(function(DecorationStyles) {
    DecorationStyles[DecorationStyles["DefaultDimension"] = 16] = "DefaultDimension";
    DecorationStyles[DecorationStyles["MarginLeft"] = -17] = "MarginLeft";
})(DecorationStyles || (DecorationStyles = {}));
var DecorationSelector;
(function(DecorationSelector) {
    DecorationSelector["CommandDecoration"] = "terminal-command-decoration";
    DecorationSelector["Hide"] = "hide";
    DecorationSelector["ErrorColor"] = "error";
    DecorationSelector["DefaultColor"] = "default-color";
    DecorationSelector["Default"] = "default";
    DecorationSelector["Codicon"] = "codicon";
    DecorationSelector["XtermDecoration"] = "xterm-decoration";
    DecorationSelector["OverviewRuler"] = ".xterm-decoration-overview-ruler";
})(DecorationSelector || (DecorationSelector = {}));
function getTerminalDecorationHoverContent(command, hoverMessage, showCommandActions) {
    let hoverContent = showCommandActions ? `${( localize(13466, "Show Command Actions"))}\n\n---\n\n` : "";
    if (!command) {
        if (hoverMessage) {
            hoverContent = hoverMessage;
        } else {
            return "";
        }
    } else if (command.markProperties || hoverMessage) {
        if (command.markProperties?.hoverMessage || hoverMessage) {
            hoverContent = command.markProperties?.hoverMessage || hoverMessage || "";
        } else {
            return "";
        }
    } else {
        if (isNumber(command.duration)) {
            const durationText = getDurationString(command.duration);
            if (command.exitCode) {
                if (command.exitCode === -1) {
                    hoverContent += ( localize(
                        13467,
                        "Command executed {0}, took {1} and failed",
                        fromNow(command.timestamp, true),
                        durationText
                    ));
                } else {
                    hoverContent += ( localize(
                        13468,
                        "Command executed {0}, took {1} and failed (Exit Code {2})",
                        fromNow(command.timestamp, true),
                        durationText,
                        command.exitCode
                    ));
                }
            } else {
                hoverContent += ( localize(
                    13469,
                    "Command executed {0} and took {1}",
                    fromNow(command.timestamp, true),
                    durationText
                ));
            }
        } else {
            if (command.exitCode) {
                if (command.exitCode === -1) {
                    hoverContent += ( localize(13470, "Command executed {0} and failed", fromNow(command.timestamp, true)));
                } else {
                    hoverContent += ( localize(
                        13471,
                        "Command executed {0} and failed (Exit Code {1})",
                        fromNow(command.timestamp, true),
                        command.exitCode
                    ));
                }
            } else {
                hoverContent += ( localize(13472, "Command executed {0} now"));
            }
        }
    }
    return hoverContent;
}
var TerminalCommandDecorationStatus;
(function(TerminalCommandDecorationStatus) {
    TerminalCommandDecorationStatus["Unknown"] = "unknown";
    TerminalCommandDecorationStatus["Running"] = "running";
    TerminalCommandDecorationStatus["Success"] = "success";
    TerminalCommandDecorationStatus["Error"] = "error";
})(TerminalCommandDecorationStatus || (TerminalCommandDecorationStatus = {}));
const unknownText = ( localize(13473, "Unknown"));
const runningText = ( localize(13474, "Running"));
function getTerminalCommandDecorationTooltip(command, storedState) {
    if (command) {
        return getTerminalDecorationHoverContent(command);
    }
    if (!storedState) {
        return "";
    }
    const timestamp = storedState.timestamp;
    const exitCode = storedState.exitCode;
    const duration = storedState.duration;
    if (typeof timestamp !== "number" || timestamp === undefined) {
        return "";
    }
    let hoverContent = "";
    const fromNowText = fromNow(timestamp, true);
    if (typeof duration === "number") {
        const durationText = getDurationString(Math.max(duration, 0));
        if (exitCode) {
            if (exitCode === -1) {
                hoverContent += ( localize(
                    13467,
                    "Command executed {0}, took {1} and failed",
                    fromNowText,
                    durationText
                ));
            } else {
                hoverContent += ( localize(
                    13468,
                    "Command executed {0}, took {1} and failed (Exit Code {2})",
                    fromNowText,
                    durationText,
                    exitCode
                ));
            }
        } else {
            hoverContent += ( localize(13469, "Command executed {0} and took {1}", fromNowText, durationText));
        }
    } else {
        if (exitCode) {
            if (exitCode === -1) {
                hoverContent += ( localize(13470, "Command executed {0} and failed", fromNowText));
            } else {
                hoverContent += ( localize(
                    13471,
                    "Command executed {0} and failed (Exit Code {1})",
                    fromNowText,
                    exitCode
                ));
            }
        } else {
            hoverContent += ( localize(13475, "Command executed {0} ", fromNowText));
        }
    }
    return hoverContent;
}
function getTerminalCommandDecorationState(command, storedState, now = Date.now()) {
    let status = TerminalCommandDecorationStatus.Unknown;
    const exitCode = command?.exitCode ?? storedState?.exitCode;
    let exitCodeText = unknownText;
    const startTimestamp = command?.timestamp ?? storedState?.timestamp;
    let startText = unknownText;
    let durationMs;
    let durationText = unknownText;
    if (typeof startTimestamp === "number") {
        startText = ( new Date(startTimestamp)).toLocaleString();
    }
    if (command) {
        if (command.exitCode === undefined) {
            status = TerminalCommandDecorationStatus.Running;
            exitCodeText = runningText;
            durationMs = startTimestamp !== undefined ? Math.max(0, now - startTimestamp) : undefined;
        } else if (command.exitCode !== 0) {
            status = TerminalCommandDecorationStatus.Error;
            exitCodeText = String(command.exitCode);
            durationMs = command.duration ?? (startTimestamp !== undefined ? Math.max(0, now - startTimestamp) : undefined);
        } else {
            status = TerminalCommandDecorationStatus.Success;
            exitCodeText = String(command.exitCode);
            durationMs = command.duration ?? (startTimestamp !== undefined ? Math.max(0, now - startTimestamp) : undefined);
        }
    } else if (storedState) {
        if (storedState.exitCode === undefined) {
            status = TerminalCommandDecorationStatus.Running;
            exitCodeText = runningText;
            durationMs = startTimestamp !== undefined ? Math.max(0, now - startTimestamp) : undefined;
        } else if (storedState.exitCode !== 0) {
            status = TerminalCommandDecorationStatus.Error;
            exitCodeText = String(storedState.exitCode);
            durationMs = storedState.duration;
        } else {
            status = TerminalCommandDecorationStatus.Success;
            exitCodeText = String(storedState.exitCode);
            durationMs = storedState.duration;
        }
    }
    if (typeof durationMs === "number") {
        durationText = getDurationString(Math.max(durationMs, 0));
    }
    const classNames = [];
    let icon = terminalDecorationIncomplete;
    switch (status) {
    case TerminalCommandDecorationStatus.Running:
    case TerminalCommandDecorationStatus.Unknown:
        classNames.push(DecorationSelector.DefaultColor, DecorationSelector.Default);
        icon = terminalDecorationIncomplete;
        break;
    case TerminalCommandDecorationStatus.Error:
        classNames.push(DecorationSelector.ErrorColor);
        icon = terminalDecorationError;
        break;
    case TerminalCommandDecorationStatus.Success:
        classNames.push("success");
        icon = terminalDecorationSuccess;
        break;
    }
    const hoverMessage = getTerminalCommandDecorationTooltip(command, storedState);
    return {
        status,
        icon,
        classNames,
        exitCode,
        exitCodeText,
        startTimestamp,
        startText,
        duration: durationMs,
        durationText,
        hoverMessage
    };
}
function updateLayout(configurationService, element) {
    if (!element) {
        return;
    }
    const fontSize = configurationService.inspect(TerminalSettingId.FontSize).value;
    const defaultFontSize = configurationService.inspect(TerminalSettingId.FontSize).defaultValue;
    const lineHeight = configurationService.inspect(TerminalSettingId.LineHeight).value;
    if (isNumber(fontSize) && isNumber(defaultFontSize) && isNumber(lineHeight)) {
        const scalar = (fontSize / defaultFontSize) <= 1 ? (fontSize / defaultFontSize) : 1;
        element.style.width = `${scalar * DecorationStyles.DefaultDimension}px`;
        element.style.height = `${scalar * DecorationStyles.DefaultDimension * lineHeight}px`;
        element.style.fontSize = `${scalar * DecorationStyles.DefaultDimension}px`;
        element.style.marginLeft = `${scalar * DecorationStyles.MarginLeft}px`;
    }
}

export { DecorationSelector, TerminalCommandDecorationStatus, getTerminalCommandDecorationState, getTerminalCommandDecorationTooltip, getTerminalDecorationHoverContent, updateLayout };
