(require 'json)
(require 'url-util)


(defun tx/add-to-end (start end user-input)
  ""
  (interactive "r\nsEnter: ")
  (let ((selected-text (buffer-substring start end)))
    (message "Selected text: %s, User input: %s" selected-text user-input)
    (delete-region start end)
    (dolist (line (split-string selected-text "\n"))
      (insert (concat line user-input "\n")))))


(defun tx/remove-line-separator (start end)
  (interactive "r")
  (let ((selected-text (buffer-substring start end)))
    (delete-region start end)
    (dolist (line (split-string selected-text "\n"))
      (insert (string-replace "  " "" (concat line " "))))
    ))


(defun tx/add-separator-by-symbol (start end user-input)
  (interactive "r\nsEnter symbol: ")
  (let ((selected-text (buffer-substring start end)))
    (delete-region start end)
    (dolist (line (split-string selected-text user-input))
      (insert (concat line user-input "\n")))))


(defun tx/url-param-to-json (start end)
  "Convert URL query parameters from the region (START END) into JSON format."
  (interactive "r")
  (let* ((url-string (buffer-substring-no-properties start end))
         (query-part (if (string-match "\\?.*" url-string)
                         (substring url-string (+ (match-beginning 0) 1))
                       url-string))
         (params (url-parse-query-string query-part))
         (json-object (mapcar (lambda (pair)
                                (cons (car pair) (cadr pair)))
                              params)))
    (kill-new (json-encode json-object))
    (message "Converted URL parameters to JSON and copied to clipboard: %s"
             (json-encode json-object))))
