'use strict';

var eventsource$1 = require('eventsource');

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const eventsource = (url, options, terminal) => {
  const {
    initOptions,
    onOpen,
    onClose,
    onError,
    formatMessage,
    closeEventName = "close",
    openEventName = "open",
    errorEventName = "error",
    messageEventName = "message",
    onMessage
  } = options;
  let aborted = false;
  let eventSource = null;
  const close = () => {
    aborted = true;
    if (eventSource) {
      eventSource.close();
      eventSource = null;
    }
  };
  const start = () => {
    var _a;
    try {
      eventSource = new eventsource$1.EventSource(url, initOptions);
      eventSource.addEventListener(openEventName, (e) => {
        onOpen && onOpen(e, eventSource, {
          terminal
        });
      });
      eventSource.addEventListener(errorEventName, (err) => {
        var _a2;
        onError && onError(err);
        if (!aborted && options.reconnect && (eventSource == null ? void 0 : eventSource.readyState) === eventsource$1.EventSource.CLOSED) {
          const timeout = (_a2 = options.reconnectWait) != null ? _a2 : 1;
          setTimeout(start, timeout * 1e3);
        }
      });
      eventSource.addEventListener(messageEventName, (e) => {
        onMessage && onMessage(e);
        let msg = formatMessage ? formatMessage(e.data) : e.data;
        if (typeof msg !== "string") {
          return;
        }
        msg = msg.endsWith("\n") ? msg : `${msg}
`;
        terminal.write(msg);
      });
      eventSource.addEventListener(closeEventName, (e) => {
        var _a2;
        onClose == null ? void 0 : onClose(e);
        close();
        if (!aborted && options.reconnect) {
          const timeout = (_a2 = options.reconnectWait) != null ? _a2 : 1;
          setTimeout(start, timeout * 1e3);
        }
      });
    } catch (err) {
      console.error("Error connecting to EventSource:", err);
      if (!aborted && options.reconnect) {
        const timeout = (_a = options.reconnectWait) != null ? _a : 1;
        setTimeout(start, timeout * 1e3);
      }
    }
  };
  start();
  return close;
};
const macBasic = {
  ctrlKey: false,
  shiftKey: false
};
const customShortcutsForMac = [
  __spreadProps(__spreadValues({}, macBasic), { metaKey: false, key: "arrowup" }),
  __spreadProps(__spreadValues({}, macBasic), { metaKey: false, key: "arrowdown" }),
  __spreadProps(__spreadValues({}, macBasic), { metaKey: false, key: "pageup" }),
  __spreadProps(__spreadValues({}, macBasic), { metaKey: false, key: "pagedown" }),
  __spreadProps(__spreadValues({}, macBasic), { metaKey: true, key: "c" }),
  __spreadProps(__spreadValues({}, macBasic), { metaKey: true, key: "a" })
];
const winBasic = {
  metaKey: false,
  shiftKey: false
};
const customShortcutsForWin = [
  __spreadProps(__spreadValues({}, winBasic), { ctrlKey: false, key: "pageup" }),
  __spreadProps(__spreadValues({}, winBasic), { ctrlKey: false, key: "pagedown" }),
  __spreadProps(__spreadValues({}, winBasic), { ctrlKey: true, key: "arrowup" }),
  __spreadProps(__spreadValues({}, winBasic), { ctrlKey: true, key: "arrowdown" }),
  __spreadProps(__spreadValues({}, winBasic), { ctrlKey: true, key: "c" }),
  __spreadProps(__spreadValues({}, winBasic), { ctrlKey: true, key: "a" })
];
const isCustomKeyboardAction = (event, actions) => actions.some(
  (a) => a.ctrlKey === event.ctrlKey && a.shiftKey == event.shiftKey && a.metaKey === event.metaKey && a.key.toLocaleLowerCase() === event.key.toLocaleLowerCase()
);
const isMacOS = () => {
  return navigator.platform.indexOf("Mac") !== -1 || navigator.userAgent.indexOf("Mac") !== -1;
};
const scrollPages = (direction, terminal) => {
  if (terminal) {
    const pageCount = direction === "up" ? -1 : 1;
    terminal.scrollPages(pageCount);
  }
};
const scrollLines = (direction, terminal) => {
  if (terminal) {
    const lineCount = direction === "up" ? -1 : 1;
    terminal.scrollLines(lineCount);
  }
};
const selectAll = (terminal) => {
  if (terminal) {
    terminal.selectAll();
  }
};
const copySelectLines = (terminal) => {
  if (terminal) {
    navigator.clipboard.writeText(terminal.getSelection());
  }
};

exports.copySelectLines = copySelectLines;
exports.customShortcutsForMac = customShortcutsForMac;
exports.customShortcutsForWin = customShortcutsForWin;
exports.eventsource = eventsource;
exports.isCustomKeyboardAction = isCustomKeyboardAction;
exports.isMacOS = isMacOS;
exports.scrollLines = scrollLines;
exports.scrollPages = scrollPages;
exports.selectAll = selectAll;
