"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var soundNotification_exports = {};
__export(soundNotification_exports, {
  CUSTOM_SOUND_EXTENSIONS: () => CUSTOM_SOUND_EXTENSIONS,
  SoundNotificationService: () => SoundNotificationService,
  getDefaultSoundSpec: () => getDefaultSoundSpec,
  playNotificationSound: () => playNotificationSound,
  playSoundFile: () => playSoundFile
});
module.exports = __toCommonJS(soundNotification_exports);
var import_fs = require("fs");
var import_path = require("path");
var import_node_url = require("node:url");
var import_child_process = require("child_process");
var import_agent_management = require("@dexto/agent-management");
const import_meta = {};
const CUSTOM_SOUND_EXTENSIONS = [".wav", ".mp3", ".ogg", ".oga", ".aiff", ".m4a"];
function getSoundFileKey(soundType) {
  switch (soundType) {
    case "startup":
      return "startupSoundFile";
    case "approval":
      return "approvalSoundFile";
    case "complete":
      return "completeSoundFile";
  }
}
function resolvePathWithinSoundsDir(soundPath) {
  if (soundPath.trim().length === 0) return null;
  const soundsDir = (0, import_path.normalize)((0, import_agent_management.getDextoGlobalPath)("sounds"));
  const resolved = (0, import_path.isAbsolute)(soundPath) ? (0, import_path.normalize)(soundPath) : (0, import_path.resolve)(soundsDir, soundPath);
  if (resolved === soundsDir || resolved.startsWith(soundsDir + import_path.sep)) {
    return resolved;
  }
  return null;
}
const PLATFORM_SOUNDS = {
  darwin: {
    // macOS system sounds
    approval: "/System/Library/Sounds/Blow.aiff",
    complete: "/System/Library/Sounds/Glass.aiff"
  },
  linux: {
    // Common Linux sound paths (freedesktop)
    approval: "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga",
    complete: "/usr/share/sounds/freedesktop/stereo/complete.oga"
  },
  win32: {
    // Windows system sounds (handled differently via PowerShell)
    approval: "SystemAsterisk",
    complete: "SystemHand"
  }
};
const BUNDLED_STARTUP_SOUND_PATH = (0, import_node_url.fileURLToPath)(
  new URL("../../assets/sounds/startup.wav", import_meta.url)
);
function getDefaultSoundSpec(soundType) {
  if (soundType === "startup") {
    return BUNDLED_STARTUP_SOUND_PATH;
  }
  const platformSounds = PLATFORM_SOUNDS[process.platform];
  return platformSounds?.[soundType] ?? null;
}
function playSound(soundPath) {
  const execOrBell = (cmd, args, onError) => {
    (0, import_child_process.execFile)(cmd, args, { windowsHide: true }, (error) => {
      if (!error) return;
      if (onError) {
        onError();
        return;
      }
      playTerminalBell();
    });
  };
  switch (process.platform) {
    case "darwin": {
      execOrBell("afplay", [soundPath]);
      return;
    }
    case "linux": {
      const lowerSoundPath = soundPath.toLowerCase();
      const isOgg = lowerSoundPath.endsWith(".oga") || lowerSoundPath.endsWith(".ogg");
      if (isOgg) {
        execOrBell("paplay", [soundPath], () => {
          execOrBell("ogg123", ["-q", soundPath]);
        });
      } else {
        execOrBell("paplay", [soundPath], () => {
          execOrBell("aplay", ["-q", soundPath]);
        });
      }
      return;
    }
    case "win32": {
      if (["SystemAsterisk", "SystemHand", "SystemExclamation", "SystemQuestion"].includes(
        soundPath
      )) {
        const systemSoundName = soundPath.replace("System", "");
        execOrBell("powershell", [
          "-NoProfile",
          "-NonInteractive",
          "-Command",
          `[System.Media.SystemSounds]::${systemSoundName}.Play()`
        ]);
        return;
      }
      execOrBell("powershell", [
        "-NoProfile",
        "-NonInteractive",
        "-Command",
        "& { param([string]$p) (New-Object System.Media.SoundPlayer $p).PlaySync() }",
        soundPath
      ]);
      return;
    }
    default: {
      playTerminalBell();
      return;
    }
  }
}
function playSoundFile(soundPath) {
  playSound(soundPath);
}
function playTerminalBell() {
  process.stdout.write("\x07");
}
function resolveNotificationSoundSpec(soundType, config) {
  const configuredRelativePath = config?.[getSoundFileKey(soundType)];
  if (configuredRelativePath) {
    const resolved = resolvePathWithinSoundsDir(configuredRelativePath);
    if (resolved && (0, import_fs.existsSync)(resolved)) {
      return resolved;
    }
  }
  const defaultSpec = getDefaultSoundSpec(soundType);
  if (!defaultSpec) return null;
  if (process.platform === "win32") {
    return defaultSpec;
  }
  return (0, import_fs.existsSync)(defaultSpec) ? defaultSpec : null;
}
function playNotificationSound(soundType, config) {
  const soundSpec = resolveNotificationSoundSpec(soundType, config);
  if (!soundSpec) {
    playTerminalBell();
    return;
  }
  playSound(soundSpec);
}
class SoundNotificationService {
  config;
  constructor(config) {
    this.config = { ...config };
  }
  /**
   * Update configuration
   */
  setConfig(config) {
    this.config = { ...this.config, ...config };
  }
  /**
   * Get current configuration
   */
  getConfig() {
    return { ...this.config };
  }
  /**
   * Play CLI startup sound if enabled
   */
  playStartupSound() {
    if (this.config.enabled && this.config.onStartup) {
      playNotificationSound("startup", this.config);
    }
  }
  /**
   * Play approval required sound if enabled
   */
  playApprovalSound() {
    if (this.config.enabled && this.config.onApprovalRequired) {
      playNotificationSound("approval", this.config);
    }
  }
  /**
   * Play task complete sound if enabled
   */
  playCompleteSound() {
    if (this.config.enabled && this.config.onTaskComplete) {
      playNotificationSound("complete", this.config);
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CUSTOM_SOUND_EXTENSIONS,
  SoundNotificationService,
  getDefaultSoundSpec,
  playNotificationSound,
  playSoundFile
});
