import * as semver from 'semver'
import { getScript_script } from '../graphql/types/getScript'
import { getPackage_package } from '../graphql/types/getPackage'

export function applyScriptToPackage(
  script: getScript_script,
  pkg: getPackage_package
): { data: getPackage_package; version: string } {
  const data = {
    ...pkg,
    description: script.description,
    readme: script.readme,
    category: script.category,
  }

  // latest version
  const [version] = semver.rsort(pkg.scriptMetadata.map(m => m.version))

  return { data, version }
}

export function targetToString(target: Partial<getPackage_package>): string {
  const { name, namespace } = target

  if (namespace && namespace !== 'none') {
    return `${namespace}/${name}`
  } else {
    return name
  }
}
