text: var Haikunator = require('haikunator')
  
  const getJiraConfiguration = (jiraSettings) => {
    var jiraApi = jiraSettings.api || {};
    var url = undefined;
  
    if(jiraApi.url && jiraApi.url.toLowerCase().includes('https://')) {
      url = jiraApi.url.replace('https://', '')
    }
  
    if(jiraApi.url && jiraApi.url.toLowerCase().includes('http://')){
      url = jiraApi.url.replace('http://', '')
    }
  
    return {
      // API
      api: {
        // Root host of your JIRA installation without protocol.
        // (i.e 'atlassian.net')
        host: url || undefined,
        // Email address of the user to login with
        email: jiraApi.email || undefined,
        // Auth token of the user to login with
        // https://confluence.atlassian.com/cloud/api-tokens-938839638.html
        token: jiraApi.token || undefined,
        // If you need to set some jira-client option use this object. 
        // Check jira-client docs for available options: https://jira-node.github.io/typedef/index.html#static-typedef-JiraApiOptions
        options: jiraApi.options || {},
      },
      //https://atlassian.net
      baseUrl: jiraApi.url || undefined,
      // Regex used to match the issue ticket key
      // Use capture group one to isolate the key text within surrounding characters (if needed).
      ticketIDPattern: jiraSettings.ticketIDPattern || /\[([A-Z]+\-[0-9]+)\]/i,
  
      // Status names that mean the ticket is approved.
      approvalStatus: jiraSettings.approvalStatus || ['Done', 'Closed', 'Accepted'],
  
      // Tickets to exclude from the changelog, by type name
      excludeIssueTypes: jiraSettings.excludeIssueTypes || ['Sub-task', 'Story Bug'],
  
      // Tickets to include in changelog, by type name.
      // If this is defined, `excludeIssueTypes` is ignored.
      includeIssueTypes: jiraSettings.includeIssueTypes || []
    }
  };
  
  const getSlackSettings = (slackSettings) => {
    return {
        // API key string
        apiKey: slackSettings.apiKey || undefined,
  
        // The channel that the changelog will be posted in, when you use the `--slack` flag.
        // This can be a channel string ('#mychannel`) or a channel ID.
        channel: slackSettings.channel || undefined,
  
        // The name to give the slack bot user, when posting the changelog
        username: slackSettings.username || "Changelog Bot",
  
        // Emoji to use for the bot icon.
        // Cannot be used at the same time as `icon_url`
        icon_emoji: slackSettings.icon_emoji || ":clipboard:",
  
        // URL to an image to use as the icon for the bot.
        // Cannot be used at the same time as `icon_emoji`
        icon_url: slackSettings.icon_url || undefined
    }
  };
  
  const getGithubSettings = (githubSettings) => {
    var rangeSettings = githubSettings.defaultRange || {};
    return {
      // Default range for commits.
      // This can include from/to git commit references
      // and or after/before datestamps.
      defaultRange: {
        from: rangeSettings.from || "origin/prod",
        to: rangeSettings.to || "origin/stage",
  
        // symmetric='...'
        // non-symmetric='..'
        // https://matthew-brett.github.io/pydagogue/git_diff_dots.html
        symmetric: false,
      }
    };
  };
  
  const getTemplateSettings = (templateSettings) => {
    var newTemplate = (templateSettings.layout && 
      templateSettings.layout.length > 0 && 
      typeof templateSettings.layout == 'string') ? templateSettings.layout : undefined;
    return {
      includePendingApprovalSection: templateSettings.includePendingApprovalSection || false,
      includeOtherCommits: templateSettings.includeOtherCommits || false,
      hideEmptyBlocks: templateSettings.hideEmptyBlocks || false,
      // The template that generates the output, as an ejs template.
      // Learn more: http://ejs.co/
      template: newTemplate ||
      ` <% if (jira.releaseVersions && jira.releaseVersions.length) {  %>
          Release version: <%= jira.releaseVersions[0].name -%>
          <% jira.releaseVersions.sort((a, b) => a.projectKey.localeCompare(b.projectKey)).forEach((release) => { %>
            * [<%= release.projectKey %>](<%= jira.baseUrl + '/projects/' + release.projectKey + '/versions/' + release.id %>) <% -%>
          <% }); -%>
        <% } %>
        Jira Tickets
        ---------------------
        <% tickets.all.forEach((ticket) => { %>
          * [<%= ticket.fields.issuetype.name %>] - [<%= ticket.key %>](<%= jira.baseUrl + '/browse/' + ticket.key %>) <%= ticket.fields.summary -%>
        <% }); -%>
        <% if (!tickets.all.length && !options.hideEmptyBlocks) {%> ~ None ~ <% } %>
  
        <% if (options.includeOtherCommits) {%>
          Other Commits
          ---------------------
          <% commits.noTickets.forEach((commit) => { %>
            * <%= commit.slackUser ? '@'+commit.slackUser.name : commit.authorName %> - [<%= commit.revision.substr(0, 7) %>] - <%= commit.summary -%>
          <% }); -%>
          <% if (!commits.noTickets.length) {%> ~ None ~ <% } %>
        <% } %>
  
        <% if (options.includePendingApprovalSection) { %>
          Pending Approval
          ---------------------
          <% tickets.pendingByOwner.forEach((owner) => { %>
          <%= (owner.slackUser) ? '@'+owner.slackUser.name : owner.email %>
          <% owner.tickets.forEach((ticket) => { -%>
            * <%= jira.baseUrl + '/browse/' + ticket.key %>
          <% }); -%>
          <% }); -%>
          <% if (!tickets.pendingByOwner.length) {%> ~ None. Yay! ~ <% } %>
        <% } %>
      `
    };
  }
  
  module.exports = {
  
    generateReleaseVersionName: () => {
      const hasVersion = process.env.VERSION;
      if (hasVersion) {
        return hasVersion;
      } else {
        const haikunator = new Haikunator();
        return haikunator.haikunate();
      }
    },
  
    getConfiguration: (jira, slackSettings, github, template) => {
      return { 
        jira:{ ...getJiraConfiguration(jira) }, 
        slack: { ...getSlackSettings(slackSettings) }, 
        sourceControl: { ...getGithubSettings(github) },
        display: { ...getTemplateSettings(template) }
      }
    }
  };
  