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

/** @module
  @summary Send iMessages through the macOS Messages app.
  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
}

@always(to)
effect std::sendIMessage { to: string }

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

  @param to - The recipient's phone number or iMessage email address.
  @param message - Non-empty message text.
  @param allowList - Limit recipients to these addresses or numbers, or leave empty for no allow restriction.
  @param blockList - Reject these addresses or numbers even if they are allowed.
  """
  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
    })
  }
}
