import * as md5 from "md5";

export interface IParsedPackageTarget {
  name: string;
  namespace?: string;
  version?: string;
}

export function parsePackageTarget(target: string): IParsedPackageTarget {
  let name: string;
  let namespace: string;
  let version: string;

  [target, version] = target.split("@");
  const [p1, p2] = target.split("/");

  if (p2) {
    namespace = p1;
    name = p2;
  } else {
    name = p1;
  }

  return {
    name,
    namespace,
    version,
  };
}

export function createPackageHash({ name, namespace }: IParsedPackageTarget) {
  return md5((namespace || "none") + name);
}

export function ddbArrayNormalizer(value: undefined | null | any[]) {
  if (Array.isArray(value) && value.length) {
    return value;
  } else {
    return null;
  }
}
