<commit_before>import {
  Relationships,
  Groups,
  Individual,
  MapaIndividualTicket,
  Group,
} from "../types";
import * as turf from "@turf/turf";

export const getSelectValues = (values: {
  [x: string]: { value: unknown; label: string } & string;
}): {
  [x: string]: string | number;
} =>
  Object.keys(values).reduce((newObj, old) => {
    const newValue =
      typeof values[old] === "object" && values[old] !== null
        ? values[old].value
        : values[old];
    return {
      ...newObj,
      [old]: newValue,
    };
  }, {});

export const getStates = (): Array<{ label: string; value?: string }> => [
  { label: "AC", value: "ac" },
  { label: "AL", value: "al" },
  { label: "AP", value: "ap" },
  { label: "AM", value: "am" },
  { label: "BA", value: "ba" },
  { label: "CE", value: "ce" },
  { label: "DF", value: "df" },
  { label: "ES", value: "es" },
  { label: "GO", value: "go" },
  { label: "MA", value: "ma" },
  { label: "MT", value: "mt" },
  { label: "MS", value: "ms" },
  { label: "MG", value: "mg" },
  { label: "PA", value: "pa" },
  { label: "PB", value: "pb" },
  { label: "PR", value: "pr" },
  { label: "PE", value: "pe" },
  { label: "PI", value: "pi" },
  { label: "RJ", value: "rj" },
  { label: "RN", value: "rn" },
  { label: "RS", value: "rs" },
  { label: "RO", value: "ro" },
  { label: "RR", value: "rr" },
  { label: "SC", value: "sc" },
  { label: "SP", value: "sp" },
  { label: "SE", value: "se" },
  { label: "TO", value: "to" },
];

export const encodeText = (input: string): string => encodeURIComponent(input);

export const parseNumber = (input: string): string => input.replace(/\D/g, "");

/**
 * @param name Inital letter that will agreggate with propriety name
 * @param obj Object that contains all key/value pairs
 * @return {"VFIRST_NAME": Test, "VEMAIL: "<EMAIL>"}
 */
export const dicio = (
  name: string,
  obj: Record<string, any>
): Record<string, any> =>
  Object.keys(obj).reduce((acumulator, k) => {
    const key = k !== "agent" ? name + k : k;
    return {
      ...acumulator,
      [key.toUpperCase()]: obj[k],
    };
  }, {});

/**
 * @param msg Message set in rede_group settings
 * @param dicio Dicionary made to transpile msg using volunteer/recipient data
 * @return { string }
 */
export const customText = (msg = "", dicio: any): string => {
  const re = new RegExp(Object.keys(dicio).join("|"), "gi");

  return msg.replace(re, (matched) => dicio[matched]);
};

export const whatsappLink = (number: string, text: string): string =>
  `https://web.whatsapp.com/send?phone=55${number}&text=${encodeURIComponent(
    text
  )}`;

export const createCustomWhatsappLink = (
  {
    volunteer,
    recipient,
  }: {
    volunteer: Omit<Individual, "userStatus" | "ticketId" | "externalId">;
    recipient: Individual;
  },
  agent: string
): {
  volunteer: string;
  recipient: string;
} => {
  if (volunteer && recipient) {
    const messageDicio = {
      ...dicio("v", { ...volunteer, agent }),
      ...dicio("i", { ...recipient, agent }),
    };

    const volunteerText = customText(
      volunteer.group?.settings?.communication?.whatsapp,
      messageDicio
    );
    const recipientText = customText(
      recipient.group?.settings?.communication?.whatsapp,
      messageDicio
    );

    return {
      volunteer: whatsappLink(
        volunteer.whatsapp || volunteer.phone || "0",
        volunteerText
      ),
      recipient: whatsappLink(
        recipient.whatsapp || recipient.phone || "0",
        recipientText
      ),
    };
  }
  return { volunteer: "", recipient: "" };
};

type TicketsWithUsers = Array<{
  recipient: {<commit_msg>fix(redes): register_occupation to render whatsapp message<commit_after>import {
  Relationships,
  Groups,
  Individual,
  MapaIndividualTicket,
  Group,
} from "../types";
import * as turf from "@turf/turf";

export const getSelectValues = (values: {
  [x: string]: { value: unknown; label: string } & string;
}): {
  [x: string]: string | number;
} =>
  Object.keys(values).reduce((newObj, old) => {
    const newValue =
      typeof values[old] === "object" && values[old] !== null
        ? values[old].value
        : values[old];
    return {
      ...newObj,
      [old]: newValue,
    };
  }, {});

export const getStates = (): Array<{ label: string; value?: string }> => [
  { label: "AC", value: "ac" },
  { label: "AL", value: "al" },
  { label: "AP", value: "ap" },
  { label: "AM", value: "am" },
  { label: "BA", value: "ba" },
  { label: "CE", value: "ce" },
  { label: "DF", value: "df" },
  { label: "ES", value: "es" },
  { label: "GO", value: "go" },
  { label: "MA", value: "ma" },
  { label: "MT", value: "mt" },
  { label: "MS", value: "ms" },
  { label: "MG", value: "mg" },
  { label: "PA", value: "pa" },
  { label: "PB", value: "pb" },
  { label: "PR", value: "pr" },
  { label: "PE", value: "pe" },
  { label: "PI", value: "pi" },
  { label: "RJ", value: "rj" },
  { label: "RN", value: "rn" },
  { label: "RS", value: "rs" },
  { label: "RO", value: "ro" },
  { label: "RR", value: "rr" },
  { label: "SC", value: "sc" },
  { label: "SP", value: "sp" },
  { label: "SE", value: "se" },
  { label: "TO", value: "to" },
];

export const encodeText = (input: string): string => encodeURIComponent(input);

export const parseNumber = (input: string): string => input.replace(/\D/g, "");

/**
 * @param name Inital letter that will agreggate with propriety name
 * @param obj Object that contains all key/value pairs
 * @return {"VFIRST_NAME": Test, "VEMAIL: "<EMAIL>"}
 */
export const dicio = (
  name: string,
  obj: Record<string, any>
): Record<string, any> =>
  Object.keys(obj).reduce((acumulator, k) => {
    const key = k !== "agent" ? name + k : k;
    return {
      ...acumulator,
      [key.toUpperCase()]: obj[k],
    };
  }, {});

/**
 * @param msg Message set in rede_group settings
 * @param dicio Dicionary made to transpile msg using volunteer/recipient data
 * @return { string }
 */
export const customText = (msg = "", dicio: any): string => {
  const re = new RegExp(Object.keys(dicio).join("|"), "gi");

  return msg.replace(re, (matched) => dicio[matched]);
};

export const whatsappLink = (number: string, text: string): string =>
  `https://web.whatsapp.com/send?phone=55${number}&text=${encodeURIComponent(
    text
  )}`;

type MathArgs = {
  volunteer: Omit<Individual, "userStatus" | "ticketId" | "externalId">;
  recipient: Individual
}

type Result = {
  volunteerUrl: string;
  recipientUrl: string
}

export const createCustomWhatsappLink = (
  { volunteer, recipient }: MathArgs,
  agentName: string
): Result => {
  if (volunteer && recipient) {
    const messageDicio = {
      ...dicio("v", {
        ...volunteer,
        registeroccupation: volunteer.extras?.register_occupation,
        agent: agentName
      }),
      ...dicio("i", { ...recipient, agent: agentName }),
    };

    const volunteerText = customText(
      volunteer.group?.settings?.communication?.whatsapp,
      messageDicio
    );
    const recipientText = customText(
      recipient.group?.settings?.communication?.whatsapp,
      messageDicio
    );

    return {
      volunteerUrl: whatsappLink(
        volunteer.whatsapp || volunteer.phone || "0",
        volunteerText
      ),
      recipientUrl: whatsappLink(
        recipient.whatsapp || recipient.phone || "0",
        recipientText
      ),
    };
  }
  return { volunteerUrl: "", recipientUrl: "" };
};

type TicketsWithUsers = Array<{
  recipient: {
{'fraction_non_alphanumeric': 0.19573048699132756, 'fraction_numerical': 0.001067378252168112, 'mean_word_length': 3.1186813186813187, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '7315072', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2600, 'n_words': 661}