# =branch: paste ticket text, get a git branch name and a commit-message stub.
#
#   =branch PROJ-123: Fix login redirect loop on Safari
#   line 1: proj-123-fix-login-redirect-loop-on-safari      (the branch name)
#   line 2: proj-123: fix login redirect loop on safari     (commit stub)
#
# Pure transform: no command, no network, no script. Press Enter to copy both lines.
#
# What it does, in order:
#   - lowercases everything with the `lower` op (a real op, so no workaround is needed)
#   - keeps a ticket key such as PROJ-123 together, then turns every other run of
#     characters that are not a-z or 0-9 into one space
#   - writes the text twice; line 1 becomes the branch (spaces to hyphens), line 2 the stub
#   - the branch keeps at most 12 words and 60 characters, the stub at most 100 characters
#
# Known limits, on purpose:
#   - There is NO per-line case or per-line lowercase op, and the `slug`, `kebab` ops are
#     reserved (they do not run yet). So this uses `lower` plus regex steps instead.
#     Because `lower` acts on the whole text, the commit stub is lowercase too
#     (PROJ-123 becomes proj-123). Retype the case if your team wants it.
#   - Only ASCII letters and digits survive. Accented or non-Latin letters are dropped.
#   - A ticket key anywhere in the text keeps its hyphen (proj-123, utf-8); a colon is
#     added after it only when it is the first word of the stub.
#   - The branch name has no type prefix (feature/, fix/). Add one after copying.
schema_version: 1
runtime: transform
id: branch
chip: branch
title: Branch name
icon: git-branch
description: Turn pasted ticket text into a git branch name and a commit-message stub (pure, on-device)
placeholder_examples: ["PROJ-123: Fix login redirect loop on Safari"]
emits: Generic

transform:
  pipe:
    - trim
    - lower
    # a literal ~ in the input would collide with the ticket-key marker below
    - replace: { from: '~', to: ' ' }
    # keep a KEY-123 style token together by swapping its hyphen for ~ (undone at the end)
    - replace: { from: '\b([a-z][a-z0-9]*)-(\d+)\b', to: '$1~$2' }
    # everything that is not a-z, 0-9 or ~ becomes one space
    - replace: { from: '[^a-z0-9~]+', to: ' ' }
    - trim
    # line 1 = branch, line 2 = commit stub
    - template: "{value}\n{value}"
    # stub only: a leading ticket key gets a colon after it
    - replace: { from: '\n([a-z][a-z0-9]*~[0-9]+) ', to: "\n$1: " }
    # branch only: spaces to hyphens, one word per step (first 12 words), then drop the rest
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) ', to: '$1-' }
    - replace: { from: '^([^ \n]*) [^\n]*', to: '$1' }
    # length caps: branch 60 characters, stub 100 characters
    - replace: { from: '^([^\n]{1,60})[^\n]*\n', to: "$1\n" }
    - replace: { from: '\n([^\n]{1,100})[^\n]*$', to: "\n$1" }
    # a cut can leave a trailing hyphen on the branch
    - replace: { from: '-+\n', to: "\n" }
    # give the ticket key its hyphen back, on both lines
    - replace: { from: '~', to: '-' }
    # empty input
    - replace: { from: '^\n$', to: 'Paste ticket text' }

presentation:
  widget: list
  title_field: line
