<commit_before>import {compile} from 'hogan.js';
import {Dictionary} from 'lodash';

import {makeBTDModule} from '../types/betterTweetDeck/btdCommonTypes';

export enum BTDUsernameFormat {
  /** `Fullname @username` (default) */
  DEFAULT = 'both',
  /** `@username Fullname` */
  USER_FULL = 'inverted',
  /** `@username` */
  USER = 'username',
  /** `Fullname */
  FULL = 'fullname',
}

const replaceInTemplate = (
  templates: Dictionary<string>,
  templateName: string,
  ...replaceArgs: [string | RegExp, string]
) => {
  templates[`${templateName}.mustache`] = templates[`${templateName}.mustache`].replace(
    ...replaceArgs
  );
};

const Hogan = window.Hogan as {
  compile: compile;
};

export const maybeChangeUsernameFormat = makeBTDModule(({settings, TD}) => {
  const {usernamesFormat} = settings;

  TD.globalRenderOptions.btd = {
    // Use with
    // {{#btd.usernameFromURL}}myProfileURL{{/btd.usernameFromURL}}
    usernameFromURL: function usernameFromURL() {
      return function what(input, render) {
        // I don't want this function to break horribly the day Twitter closes this issue
        // https://github.com/twitter/hogan.js/issues/222#issuecomment-106101791
        const val = render ? render(input) : Hogan.compile(input).render(this);

        return (
          val.match(/https:\/\/(?:www.|)twitter.com\/(?:@|)([A-Za-z0-9_]+)/) &&
          val.match(/https:\/\/(?:www.|)twitter.com\/(?:@|)([A-Za-z0-9_]+)/)[1]
        );
      };
    },
  };

  const replaceWrapper = (templateName: string, ...replaceArgs: [string | RegExp, string]) =>
    replaceInTemplate(TD.mustaches, templateName, ...replaceArgs);

  if (usernamesFormat === 'username' || usernamesFormat === 'inverted') {
    // general fullname template
    replaceWrapper(
      'text/user_link_fullname',
      '{{#unsafe}}{{{name}}}{{/unsafe}}{{^unsafe}}{{name}}{{/unsafe}}',
      '{{#btd.usernameFromURL}}{{profileUrl}}{{/btd.usernameFromURL}}'
    );<commit_msg>better typing<commit_after>import {compile} from 'hogan.js';
import {Dictionary} from 'lodash';

import {makeBTDModule} from '../types/betterTweetDeck/btdCommonTypes';

export enum BTDUsernameFormat {
  /** `Fullname @username` (default) */
  DEFAULT = 'both',
  /** `@username Fullname` */
  USER_FULL = 'inverted',
  /** `@username` */
  USER = 'username',
  /** `Fullname */
  FULL = 'fullname',
}

const replaceInTemplate = (
  templates: Dictionary<string>,
  templateName: string,
  ...replaceArgs: [string | RegExp, string]
) => {
  templates[`${templateName}.mustache`] = templates[`${templateName}.mustache`].replace(
    ...replaceArgs
  );
};

declare global {
  interface Window {
    Hogan: {
      compile: typeof compile;
    };
  }
}

const Hogan = window.Hogan;

export const maybeChangeUsernameFormat = makeBTDModule(({settings, TD}) => {
  const {usernamesFormat} = settings;

  TD.globalRenderOptions.btd = {
    // Use with
    // {{#btd.usernameFromURL}}myProfileURL{{/btd.usernameFromURL}}
    usernameFromURL: function usernameFromURL() {
      return function what(input, render) {
        // I don't want this function to break horribly the day Twitter closes this issue
        // https://github.com/twitter/hogan.js/issues/222#issuecomment-106101791
        const val = render ? render(input) : Hogan.compile(input).render(this);

        return (
          val.match(/https:\/\/(?:www.|)twitter.com\/(?:@|)([A-Za-z0-9_]+)/) &&
          val.match(/https:\/\/(?:www.|)twitter.com\/(?:@|)([A-Za-z0-9_]+)/)[1]
        );
      };
    },
  };

  const replaceWrapper = (templateName: string, ...replaceArgs: [string | RegExp, string]) =>
    replaceInTemplate(TD.mustaches, templateName, ...replaceArgs);

  if (usernamesFormat === 'username' || usernamesFormat === 'inverted') {
    // general fullname template
    replaceWrapper(
      'text/user_link_fullname',
      '{{#unsafe}}{{{name}}}{{/unsafe}}{{^unsafe}}{{name}}{{/unsafe}}',
      '{{#btd.usernameFromURL}}{{profileUrl}}{{/btd.usernameFromURL}}'
    );
{'fraction_non_alphanumeric': 0.18848167539267016, 'fraction_numerical': 0.008476689104961356, 'mean_word_length': 3.4876957494407157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3690427', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1285, 'n_words': 234}