import { _sendIMessage } from "agency-lang/stdlib-lib/imessage.js"

/** @module
  Send an iMessage from Agency code via the macOS Messages app. Works on
  macOS only, with Messages.app signed in to iMessage. No API key required.

  ```ts
  import { sendIMessage } from "std::messaging/imessage"

  node main() {
    const result = sendIMessage("+15551234567", "Hello from my agent!")
    print(result)
  }
  ```
*/

type IMessageResult = {
  sent: boolean
}

effect std::sendIMessage { to: string }

/** Only works on macOS with Messages.app signed in to iMessage. No API key required. */
export def sendIMessage(to: string, message: string, allowList: string[] = [], blockList: string[] = []): Result {
  """
  Send an iMessage via the macOS Messages app.

  @param to - Phone number or email of the recipient
  @param message - The text to send
  @param allowList - Only allow sending to these addresses/numbers
  @param blockList - Block sending to these addresses/numbers
  """
  return interrupt std::sendIMessage("Are you sure you want to send this iMessage?", {
    to: to
  })

  destructive {
    return try _sendIMessage(to, message, {
      allowList: allowList,
      blockList: blockList
    })
  }
}
