import { ChartTemplateSpecialType } from '../types';

const BUILT_IN = ['.Release', '.Chart', '.Files', '.Capabilities', '.Template'];
const FUNCTIONS = [
  '.Capabilities.APIVersions.Has',
  'abbrev',
  'abbrevboth',
  'add',
  'add1',
  'adler32sum',
  'ago',
  'and',
  'append',
  'atoi',
  'b32dec',
  'b32enc',
  'b64dec',
  'b64enc',
  'base',
  'buildCustomCert',
  'camelcase',
  'cat',
  'ceil',
  'clean',
  'coalesce',
  'compact',
  'concat',
  'contains',
  'date',
  'dateInZone',
  'dateModify',
  'decryptAES',
  'deepCopy',
  'deepEqual',
  'default',
  'derivePassword',
  'dict',
  'dir',
  'div',
  'duration',
  'durationRound',
  'empty',
  'encryptAES',
  'eq',
  'ext',
  'fail',
  'first',
  'float64',
  'floor',
  'ge',
  'genCA',
  'genPrivateKey',
  'genSelfSignedCert',
  'genSignedCert',
  'get',
  'getHostByName',
  'gt',
  'has',
  'hasKey',
  'hasPrefix',
  'hasSuffix',
  'htmlDate',
  'htmlDateInZone',
  'htpasswd',
  'indent',
  'initial',
  'initials',
  'int',
  'int64',
  'isAbs',
  'kebabcase',
  'keys',
  'kindIs',
  'kindOf',
  'last',
  'le',
  'len',
  'lookup',
  'lower',
  'lt',
  'max',
  'merge',
  'mergeOverwrite',
  'min',
  'mod',
  'mul',
  'mustAppend',
  'mustCompact',
  'mustDateModify',
  'mustDeepCopy',
  'mustFirst',
  'mustHas',
  'mustInitial',
  'mustLast',
  'mustMerge',
  'mustMergeOverwrite',
  'mustPrepend',
  'mustRegexFind',
  'mustRegexFindAll',
  'mustRegexMatch',
  'mustRegexReplaceAll',
  'mustRegexReplaceAllLiteral',
  'mustRegexSplit',
  'mustRest',
  'mustReverse',
  'mustSlice',
  'mustToDate',
  'mustToJson',
  'mustToPrettyJson',
  'mustToRawJson',
  'mustUniq',
  'mustWithout',
  'ne',
  'nindent',
  'nospace',
  'not',
  'now',
  'omit',
  'or',
  'pick',
  'pluck',
  'plural',
  'prepend',
  'print',
  'printf',
  'println',
  'quote',
  'randAlpha',
  'randAlphaNum',
  'randAscii',
  'randNumeric',
  'regexFind',
  'regexFindAll',
  'regexMatch',
  'regexReplaceAll',
  'regexReplaceAllLiteral',
  'regexSplit',
  'repeat',
  'replace',
  'rest',
  'reverse',
  'round',
  'semver',
  'semverCompare',
  'seq',
  'set',
  'sha1sum',
  'sha256sum',
  'shuffle',
  'slice',
  'snakecase',
  'squote',
  'sub',
  'substr',
  'swapcase',
  'title',
  'toDate',
  'toDecimal',
  'toJson',
  'toPrettyJson',
  'toRawJson',
  'toString',
  'toStrings',
  'trim',
  'trimAll',
  'trimPrefix',
  'trimSuffix',
  'trunc',
  'typeIs',
  'typeIsLike',
  'typeOf',
  'uniq',
  'unixEpoch',
  'unset',
  'until',
  'untilStep',
  'untitle',
  'upper',
  'urlJoin',
  'urlParse',
  'urlquery',
  'uuidv4',
  'values',
  'without',
  'wrap',
  'wrapWith',
];

const FLOW_CONTROL = [
  'if',
  'else',
  'with',
  'range',
  'define',
  'template',
  'block',
  'include',
  'required',
  'end',
  'toYaml',
  'tpl',
];

const isBuiltInObject = (code: string): boolean => {
  for (let i = 0; i < BUILT_IN.length; i++) {
    if (code.startsWith(BUILT_IN[i]) && code !== '.Capabilities.APIVersions.Has') {
      return true;
    }
  }
  return false;
};

const processHelmTemplate = (code: string): ChartTemplateSpecialType | null => {
  if (code.startsWith('.Values')) {
    return ChartTemplateSpecialType.ValuesBuiltInObject;
  } else if (code.startsWith('"')) {
    return ChartTemplateSpecialType.String;
  } else if (code.startsWith('$')) {
    return ChartTemplateSpecialType.Variable;
  } else if (FUNCTIONS.includes(code)) {
    return ChartTemplateSpecialType.Function;
  } else if (FLOW_CONTROL.includes(code)) {
    return ChartTemplateSpecialType.FlowControl;
  } else if (isBuiltInObject(code)) {
    return ChartTemplateSpecialType.BuiltInObject;
  }

  return null;
};

export default processHelmTemplate;
