{
  "$schema": "https://schema.spbx.tel/v1/structs.realm.lib",
  "description": "Call parking: one instance = one park slot. The park export parks the current call with hold audio; the fetch export picks it up from any phone (busy tone if the slot is empty).",
  "params": {
    "slot": {
      "type": "string",
      "description": "Slot key: the parked call id is stored in the global variable park_slot_<slot>."
    }
  },
  "exports": {
    "park": {
      "description": "Park the current call: hold audio until someone fetches it.",
      "flow": "flows/sub_flow/park"
    },
    "fetch": {
      "description": "Pick up the call parked in this slot.",
      "flow": "flows/sub_flow/fetch"
    }
  },
  "uses": {
    "std": {
      "preset": "stdlib"
    }
  },
  "flows": {
    "silence_player": {
      "hold": {}
    },
    "wait_for_listener_to_start": {
      "parked": {
        "timeout_seconds": 180
      }
    },
    "hangup": {
      "done": {
        "reason": "park: parked call ended"
      },
      "empty": {
        "reason": "park: slot empty"
      }
    },
    "set_variable": {
      "claim": {
        "variable_name": "park_slot_${slot}",
        "variable_value": "%call_id%",
        "global": true
      },
      "release": {
        "variable_name": "park_slot_${slot}",
        "variable_value": "free",
        "global": true
      },
      "seed": {
        "variable_name": "park_slot_${slot}_val",
        "variable_value": "free"
      }
    },
    "set_hangup_task": {
      "release_on_end": {
        "dial_plan": [
          "flows/set_variable/release"
        ]
      }
    },
    "copy_variable": {
      "load": {
        "source_variable": "park_slot_${slot}",
        "source_global": true,
        "destination_variable": "park_slot_${slot}_val"
      }
    },
    "listen_to_call": {
      "pull": {
        "call_ids": [
          "%park_slot_${slot}_val%"
        ]
      }
    },
    "send_audio_to_call": {
      "push": {
        "call_ids": [
          "%park_slot_${slot}_val%"
        ]
      }
    },
    "if": {
      "fetch_if": {
        "condition": "conditions/variable_equals/free",
        "then": [
          "presets/std/busy",
          "flows/hangup/empty"
        ],
        "else": [
          "flows/sub_flow/grab"
        ]
      }
    },
    "sub_flow": {
      "park": {
        "dial_plan": [
          "presets/std/answer",
          "flows/set_variable/claim",
          "flows/set_hangup_task/release_on_end",
          "flows/silence_player/hold",
          "flows/wait_for_listener_to_start/parked",
          "presets/std/stop_silence",
          "presets/std/wait_for_listener_stop",
          "flows/hangup/done"
        ]
      },
      "fetch": {
        "dial_plan": [
          "presets/std/answer",
          "flows/set_variable/seed",
          "flows/copy_variable/load",
          "flows/if/fetch_if"
        ]
      },
      "grab": {
        "dial_plan": [
          "flows/set_variable/release",
          "flows/listen_to_call/pull",
          "flows/send_audio_to_call/push",
          "presets/std/wait_for_listener_stop",
          "presets/std/hangup"
        ]
      }
    }
  },
  "conditions": {
    "variable_equals": {
      "free": {
        "variable": "park_slot_${slot}_val",
        "value": "free"
      }
    }
  }
}
