'use strict';

// xterm answers OSC 10/11/12 color queries by sending bytes back to the host.
// CTM must never persist those terminal-generated replies as provider output.
// Scope this narrowly to color reply/set payloads; leave OSC queries such as
// ]10;? and ordinary text containing rgb values untouched.
const TERMINAL_GENERATED_COLOR_REPLY_RE = /(?:\x1b\]|\u009d|\])(?:10|11|12|110|111|112);(?:rgb:[0-9a-fA-F]{1,4}\/[0-9a-fA-F]{1,4}\/[0-9a-fA-F]{1,4}|#[0-9a-fA-F]{6,12})(?:\x07|\x1b\\|\u009c|\\)?/g;

function stripLeakedTerminalGeneratedColorReplies(value) {
  const raw = String(value || '');
  return raw ? raw.replace(TERMINAL_GENERATED_COLOR_REPLY_RE, '') : raw;
}

module.exports = {
  TERMINAL_GENERATED_COLOR_REPLY_RE,
  stripLeakedTerminalGeneratedColorReplies,
};
