{
  "kind": "dsl",
  "metadata": {
    "title": "Import YouTube Chapter Text As Sequence Markers",
    "descriptionMarkdown": "Imports YouTube-style chapter lines such as `0:00 Intro` or `1:02:03 Chapter title` into the active sequence as markers. You can choose whether the chapter title becomes the marker name, the marker comment, or both, and whether existing active-sequence markers should be replaced first."
  },
  "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": "Chapter text source",
              "var": "inputMode",
              "required": true,
              "options": [
                "Paste chapter text",
                "Open text file"
              ],
              "defaultValue": "Paste chapter text"
            }
          }
        },
        "next": "b3"
      },
      "b3": {
        "type": "listPush",
        "inputs": {
          "name": "promptFields",
          "value": {
            "type": "uiPromptField",
            "inputs": {
              "kind": "MULTILINE",
              "label": "Chapter text",
              "var": "pastedChapterText",
              "defaultValue": "0:00 Intro\n0:05 Chapter Two"
            }
          }
        },
        "next": "b4"
      },
      "b4": {
        "type": "listPush",
        "inputs": {
          "name": "promptFields",
          "value": {
            "type": "uiPromptField",
            "inputs": {
              "kind": "SELECT",
              "label": "Import chapter title as",
              "var": "textTargetMode",
              "required": true,
              "options": [
                "Marker name",
                "Marker comment",
                "Marker name + comment"
              ],
              "defaultValue": "Marker name"
            }
          }
        },
        "next": "b5"
      },
      "b5": {
        "type": "listPush",
        "inputs": {
          "name": "promptFields",
          "value": {
            "type": "uiPromptField",
            "inputs": {
              "kind": "CHECKBOX",
              "label": "Replace existing sequence markers",
              "var": "replaceExistingMarkers",
              "defaultValue": false
            }
          }
        },
        "next": "b6"
      },
      "b6": {
        "type": "uiPromptForm",
        "inputs": {
          "title": "YouTube Chapter Import Options",
          "message": "Paste only the chapter lines or choose a text file that contains them.",
          "fields": {
            "var": "promptFields"
          },
          "cancelBehavior": "STOP"
        },
        "next": "b7"
      },
      "b7": {
        "type": "setVar",
        "inputs": {
          "name": "chapterText",
          "value": ""
        },
        "next": "b8"
      },
      "b8": {
        "type": "if",
        "inputs": {
          "branches": [
            {
              "condition": {
                "type": "logicCompare",
                "inputs": {
                  "op": "EQ",
                  "a": {
                    "var": "inputMode"
                  },
                  "b": "Open text file"
                }
              },
              "body": [
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "chapterText",
                    "value": {
                      "type": "readTextFile",
                      "inputs": {
                        "path": {
                          "type": "openFileDialog",
                          "inputs": {
                            "cancelBehavior": "STOP",
                            "initialDomain": "DEFAULT"
                          }
                        }
                      }
                    }
                  }
                }
              ]
            }
          ],
          "elseBody": [
            {
              "type": "setVar",
              "inputs": {
                "name": "chapterText",
                "value": {
                  "var": "pastedChapterText"
                }
              }
            }
          ]
        },
        "next": "b9"
      },
      "b9": {
        "type": "if",
        "inputs": {
          "branches": [
            {
              "condition": {
                "type": "textIsEmpty",
                "inputs": {
                  "value": {
                    "type": "textTrim",
                    "inputs": {
                      "text": {
                        "var": "chapterText"
                      }
                    }
                  }
                }
              },
              "body": [
                {
                  "type": "fail",
                  "inputs": {
                    "message": "No chapter text was provided."
                  }
                }
              ]
            }
          ]
        },
        "next": "b10"
      },
      "b10": {
        "type": "setVar",
        "inputs": {
          "name": "importedMarkerCount",
          "value": 0
        },
        "next": "b11"
      },
      "b11": {
        "type": "setVar",
        "inputs": {
          "name": "skippedInvalidLineCount",
          "value": 0
        },
        "next": "b12"
      },
      "b12": {
        "type": "setVar",
        "inputs": {
          "name": "skippedEmptyTitleCount",
          "value": 0
        },
        "next": "b13"
      },
      "b13": {
        "type": "if",
        "inputs": {
          "branches": [
            {
              "condition": {
                "var": "replaceExistingMarkers"
              },
              "body": [
                {
                  "type": "prMarkerDeleteAll",
                  "inputs": {
                    "owner": "::SEQ\\ACTIVE",
                    "onError": "STOP"
                  }
                }
              ]
            }
          ]
        },
        "next": "b14"
      },
      "b14": {
        "type": "forEachTextLine",
        "inputs": {
          "text": {
            "var": "chapterText"
          },
          "lineVar": "chapterLine",
          "indexVar": "chapterLineIndex",
          "trimLine": true,
          "skipEmpty": true
        },
        "body": [
          {
            "type": "setVar",
            "inputs": {
              "name": "lineParts",
              "value": {
                "type": "textSplit",
                "inputs": {
                  "text": {
                    "var": "chapterLine"
                  },
                  "mode": "WHITESPACE_RUN",
                  "maxParts": 2
                }
              }
            }
          },
          {
            "type": "if",
            "inputs": {
              "branches": [
                {
                  "condition": {
                    "type": "logicCompare",
                    "inputs": {
                      "op": "LT",
                      "a": {
                        "type": "listLength",
                        "inputs": {
                          "list": {
                            "var": "lineParts"
                          }
                        }
                      },
                      "b": 2
                    }
                  },
                  "body": [
                    {
                      "type": "setVar",
                      "inputs": {
                        "name": "skippedInvalidLineCount",
                        "value": {
                          "type": "mathArithmetic",
                          "inputs": {
                            "op": "ADD",
                            "a": {
                              "var": "skippedInvalidLineCount"
                            },
                            "b": 1
                          }
                        }
                      }
                    }
                  ]
                }
              ],
              "elseBody": [
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "timeToken",
                    "value": {
                      "type": "listGet",
                      "inputs": {
                        "list": {
                          "var": "lineParts"
                        },
                        "where": "FIRST"
                      }
                    }
                  }
                },
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "chapterTitle",
                    "value": {
                      "type": "listGet",
                      "inputs": {
                        "list": {
                          "var": "lineParts"
                        },
                        "where": "LAST"
                      }
                    }
                  }
                },
                {
                  "type": "if",
                  "inputs": {
                    "branches": [
                      {
                        "condition": {
                          "type": "textIsEmpty",
                          "inputs": {
                            "value": {
                              "var": "chapterTitle"
                            }
                          }
                        },
                        "body": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "skippedEmptyTitleCount",
                              "value": {
                                "type": "mathArithmetic",
                                "inputs": {
                                  "op": "ADD",
                                  "a": {
                                    "var": "skippedEmptyTitleCount"
                                  },
                                  "b": 1
                                }
                              }
                            }
                          }
                        ]
                      },
                      {
                        "condition": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "EQ",
                            "a": {
                              "type": "textIndexOf",
                              "inputs": {
                                "value": {
                                  "var": "timeToken"
                                },
                                "find": ":",
                                "end": "FIRST"
                              }
                            },
                            "b": -1
                          }
                        },
                        "body": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "skippedInvalidLineCount",
                              "value": {
                                "type": "mathArithmetic",
                                "inputs": {
                                  "op": "ADD",
                                  "a": {
                                    "var": "skippedInvalidLineCount"
                                  },
                                  "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": "chapterTitle"
                                    }
                                  }
                                }
                              ]
                            },
                            {
                              "condition": {
                                "type": "logicCompare",
                                "inputs": {
                                  "op": "EQ",
                                  "a": {
                                    "var": "textTargetMode"
                                  },
                                  "b": "Marker name + comment"
                                }
                              },
                              "body": [
                                {
                                  "type": "setVar",
                                  "inputs": {
                                    "name": "markerName",
                                    "value": {
                                      "var": "chapterTitle"
                                    }
                                  }
                                },
                                {
                                  "type": "setVar",
                                  "inputs": {
                                    "name": "markerComments",
                                    "value": {
                                      "var": "chapterTitle"
                                    }
                                  }
                                }
                              ]
                            }
                          ],
                          "elseBody": [
                            {
                              "type": "setVar",
                              "inputs": {
                                "name": "markerName",
                                "value": {
                                  "var": "chapterTitle"
                                }
                              }
                            },
                            {
                              "type": "setVar",
                              "inputs": {
                                "name": "markerComments",
                                "value": ""
                              }
                            }
                          ]
                        }
                      },
                      {
                        "type": "prMarkerCreate",
                        "inputs": {
                          "owner": "::SEQ\\ACTIVE",
                          "name": {
                            "var": "markerName"
                          },
                          "comments": {
                            "var": "markerComments"
                          },
                          "type": "COMMENT",
                          "startSeconds": {
                            "type": "prTimeTextToTime",
                            "inputs": {
                              "timeText": {
                                "var": "timeToken"
                              },
                              "format": "AUTO_GENERIC"
                            }
                          },
                          "onError": "STOP"
                        }
                      },
                      {
                        "type": "setVar",
                        "inputs": {
                          "name": "importedMarkerCount",
                          "value": {
                            "type": "mathArithmetic",
                            "inputs": {
                              "op": "ADD",
                              "a": {
                                "var": "importedMarkerCount"
                              },
                              "b": 1
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        ],
        "next": "b15"
      },
      "b15": {
        "type": "uiShowMessage",
        "inputs": {
          "message": {
            "type": "textJoin",
            "inputs": {
              "items": [
                "Imported ",
                {
                  "var": "importedMarkerCount"
                },
                " marker(s). Skipped ",
                {
                  "var": "skippedInvalidLineCount"
                },
                " invalid line(s) and ",
                {
                  "var": "skippedEmptyTitleCount"
                },
                " empty-title line(s)."
              ]
            }
          }
        }
      }
    }
  }
}
