const neetoNanosFrontendPackages = {
  neetoMessageTemplates: [
    "@bigbinary/neeto-message-templates-frontend",
    "neetomessagetemplates",
  ],
  neetoProjects: ["@bigbinary/neeto-projects-frontend", "neetoprojects"],
  neetoEditor: ["@bigbinary/neeto-editor", "neetoeditor"],
  neetoFields: ["@bigbinary/neeto-fields-frontend", "neetofields"],
  neetoWebhooks: ["@bigbinary/neeto-webhooks-frontend", "neetowebhooks"],
  neetoImageUploader: [
    "@bigbinary/neeto-image-uploader-frontend",
    "neetoimageuploader",
  ],
  neetoEmailNotifications: [
    "@bigbinary/neeto-email-notifications-frontend",
    "neetoemailnotifications",
  ],
  neetoApiKeys: ["@bigbinary/neeto-api-keys-frontend", "neetoapikeys"],
  neetoTemplates: ["@bigbinary/neeto-templates-frontend", "neetotemplates"],
  neetoTags: ["@bigbinary/neeto-tags-frontend", "neetotags"],
  neetoTeam: ["@bigbinary/neeto-team-members-frontend", "neetoteam"],
  neetoFilters: ["@bigbinary/neeto-filters-frontend", "neetofilters"],
  neetoRules: ["@bigbinary/neeto-rules-frontend", "neetorules"],
  neetoForm: ["@bigbinary/neeto-form-frontend", "neetoform"],
  neetoSlack: ["@bigbinary/neeto-slack-frontend", "neetoslack"],
  neetoApprovals: ["@bigbinary/neeto-approvals-frontend", "neetoapprovals"],
  neetoIntegrations: [
    "@bigbinary/neeto-integrations-frontend",
    "neetointegrations",
  ],
  neetoGithub: ["@bigbinary/neeto-github-frontend", "neetogithub"],
  neetoThemes: ["@bigbinary/neeto-themes-frontend", "neetothemes"],
};

const createImportTransformer = basePackage => importName => {
  // Test to filter components.
  if (/^[A-Z][a-zA-Z]+$/.test(importName)) {
    return `${basePackage}/${importName}`;
  }
  if (/^use[A-Z]/.test(importName)) {
    return `${basePackage}/hooks`;
  }
  if (/^[A-Z][A-Z0-9_]+$/.test(importName)) {
    return `${basePackage}/constants`;
  }
  return `${basePackage}/utils`;
};

const generateTransformer = (packageName, aliasName) => {
  return {
    [packageName]: {
      transform: createImportTransformer(packageName),
      preventFullImport: true,
      skipDefaultConversion: true,
    },
    [aliasName]: {
      transform: createImportTransformer(aliasName),
      preventFullImport: true,
      skipDefaultConversion: true,
    },
    [`${aliasName}/([A-Z][a-zA-Z]+)$`]: {
      transform: `${aliasName}/\${member}`,
      preventFullImport: false,
    },
    [`${packageName}/([A-Z][a-zA-Z]+)$`]: {
      transform: `${packageName}/\${member}`,
      preventFullImport: false,
    },
  };
};

const generateNanosTransformers = () =>
  Object.values(neetoNanosFrontendPackages)
    .map(([packageName, aliasName]) =>
      generateTransformer(packageName, aliasName)
    )
    .reduce(
      (transformers, transformer) => ({ ...transformers, ...transformer }),
      {}
    );

module.exports = { generateNanosTransformers };
