{
  "kind": "dsl",
  "metadata": {
    "title": "Import SRT As Sequence Markers",
    "descriptionMarkdown": "Imports subtitle entries from an `.srt` file into the active sequence as sequence markers. You can choose whether each subtitle text becomes the marker name, the marker comment, or both, and you can optionally replace existing sequence markers before import. Empty subtitle text and zero- or negative-duration subtitle ranges are skipped."
  },
  "dsl": {
    "version": 0,
    "entry": "b1",
    "blocks": {
      "b1": {
        "type": "setVar",
        "inputs": {
          "name": "promptFields",
          "value": []
        },
        "next": "b2"
      },
      "b2": {
        "type": "listPush",
        "inputs": {
          "name": "promptFields",
          "value": {
            "type": "uiPromptField",
            "inputs": {
              "kind": "SELECT",
              "label": "Import subtitle text as",
              "var": "textTargetMode",
              "required": true,
              "options": [
                "Marker name",
                "Marker comment",
                "Marker name + comment"
              ],
              "defaultValue": "Marker name"
            }
          }
        },
        "next": "b3"
      },
      "b3": {
        "type": "listPush",
        "inputs": {
          "name": "promptFields",
          "value": {
            "type": "uiPromptField",
            "inputs": {
              "kind": "CHECKBOX",
              "label": "Replace existing sequence markers",
              "var": "replaceExistingMarkers",
              "defaultValue": false
            }
          }
        },
        "next": "b4"
      },
      "b4": {
        "type": "uiPromptForm",
        "inputs": {
          "title": "SRT Import Options",
          "message": "Choose how the subtitle text should be written into imported sequence markers.",
          "fields": {
            "var": "promptFields"
          },
          "cancelBehavior": "STOP"
        },
        "next": "b5"
      },
      "b5": {
        "type": "setVar",
        "inputs": {
          "name": "srtPath",
          "value": {
            "type": "openFileDialog",
            "inputs": {
              "cancelBehavior": "STOP",
              "initialDomain": "DEFAULT"
            }
          }
        },
        "next": "b6"
      },
      "b6": {
        "type": "setVar",
        "inputs": {
          "name": "importedMarkerCount",
          "value": 0
        },
        "next": "b7"
      },
      "b7": {
        "type": "setVar",
        "inputs": {
          "name": "skippedEmptyTextCount",
          "value": 0
        },
        "next": "b8"
      },
      "b8": {
        "type": "setVar",
        "inputs": {
          "name": "skippedInvalidDurationCount",
          "value": 0
        },
        "next": "b9"
      },
      "b9": {
        "type": "if",
        "inputs": {
          "branches": [
            {
              "condition": {
                "var": "replaceExistingMarkers"
              },
              "body": [
                {
                  "type": "prMarkerDeleteAll",
                  "inputs": {
                    "owner": "::SEQ\\ACTIVE",
                    "onError": "STOP"
                  }
                }
              ]
            }
          ]
        },
        "next": "b10"
      },
      "b10": {
        "type": "forEachSrtEntry",
        "inputs": {
          "path": {
            "var": "srtPath"
          },
          "indexVar": "subtitleIndex",
          "startVar": "subtitleStartText",
          "endVar": "subtitleEndText",
          "textVar": "subtitleText",
          "startSecondsVar": "subtitleStartSeconds",
          "endSecondsVar": "subtitleEndSeconds"
        },
        "body": [
          {
            "type": "if",
            "inputs": {
              "branches": [
                {
                  "condition": {
                    "type": "textIsEmpty",
                    "inputs": {
                      "value": {
                        "var": "subtitleText"
                      }
                    }
                  },
                  "body": [
                    {
                      "type": "setVar",
                      "inputs": {
                        "name": "skippedEmptyTextCount",
                        "value": {
                          "type": "mathArithmetic",
                          "inputs": {
                            "op": "ADD",
                            "a": {
                              "var": "skippedEmptyTextCount"
                            },
                            "b": 1
                          }
                        }
                      }
                    }
                  ]
                },
                {
                  "condition": {
                    "type": "logicCompare",
                    "inputs": {
                      "op": "LTE",
                      "a": {
                        "var": "subtitleEndSeconds"
                      },
                      "b": {
                        "var": "subtitleStartSeconds"
                      }
                    }
                  },
                  "body": [
                    {
                      "type": "setVar",
                      "inputs": {
                        "name": "skippedInvalidDurationCount",
                        "value": {
                          "type": "mathArithmetic",
                          "inputs": {
                            "op": "ADD",
                            "a": {
                              "var": "skippedInvalidDurationCount"
                            },
                            "b": 1
                          }
                        }
                      }
                    }
                  ]
                }
              ],
              "elseBody": [
                {
                  "type": "if",
                  "inputs": {
                    "branches": [
                      {
                        "condition": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "EQ",
                            "a": {
                              "var": "textTargetMode"
                            },
                            "b": "Marker comment"
                          }
                        },
                        "body": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "markerName",
                              "value": ""
                            }
                          },
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "markerComments",
                              "value": {
                                "var": "subtitleText"
                              }
                            }
                          }
                        ]
                      },
                      {
                        "condition": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "EQ",
                            "a": {
                              "var": "textTargetMode"
                            },
                            "b": "Marker name + comment"
                          }
                        },
                        "body": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "markerName",
                              "value": {
                                "var": "subtitleText"
                              }
                            }
                          },
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "markerComments",
                              "value": {
                                "var": "subtitleText"
                              }
                            }
                          }
                        ]
                      }
                    ],
                    "elseBody": [
                      {
                        "type": "setVar",
                        "inputs": {
                          "name": "markerName",
                          "value": {
                            "var": "subtitleText"
                          }
                        }
                      },
                      {
                        "type": "setVar",
                        "inputs": {
                          "name": "markerComments",
                          "value": ""
                        }
                      }
                    ]
                  }
                },
                {
                  "type": "prMarkerCreate",
                  "inputs": {
                    "owner": "::SEQ\\ACTIVE",
                    "name": {
                      "var": "markerName"
                    },
                    "comments": {
                      "var": "markerComments"
                    },
                    "type": "COMMENT",
                    "startSeconds": {
                      "var": "subtitleStartSeconds"
                    },
                    "durationSeconds": {
                      "type": "mathArithmetic",
                      "inputs": {
                        "op": "MINUS",
                        "a": {
                          "var": "subtitleEndSeconds"
                        },
                        "b": {
                          "var": "subtitleStartSeconds"
                        }
                      }
                    },
                    "onError": "STOP"
                  }
                },
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "importedMarkerCount",
                    "value": {
                      "type": "mathArithmetic",
                      "inputs": {
                        "op": "ADD",
                        "a": {
                          "var": "importedMarkerCount"
                        },
                        "b": 1
                      }
                    }
                  }
                }
              ]
            }
          }
        ],
        "next": "b11"
      },
      "b11": {
        "type": "uiShowMessage",
        "inputs": {
          "message": {
            "type": "textJoin",
            "inputs": {
              "items": [
                "Imported ",
                {
                  "var": "importedMarkerCount"
                },
                " sequence markers from ",
                {
                  "var": "srtPath"
                },
                ". Skipped empty text: ",
                {
                  "var": "skippedEmptyTextCount"
                },
                ". Skipped invalid duration: ",
                {
                  "var": "skippedInvalidDurationCount"
                },
                "."
              ]
            }
          }
        },
        "next": null
      }
    }
  }
}
