import {models} from "~/models/_import";

export default class ModelsUtils {
  /**
   * Extrait l'ID de l'URI passée en paramètre
   * @param uri
   */
  static extractIdFromUri (uri: string): number|null {
    if(!uri) return null

    const partUri: Array<string> = uri.split('/')
    const id:any = partUri.pop()

    if(isNaN(id))
      throw new Error('id is not a number')

    return parseInt(id)
  }
  /**
   * Extrait l'ID de l'URI passée en paramètre
   * @param iri
   */
  static getModelFromIri (iri: string): any {
    const matches = iri.match(/^\/api\/(\w+)\/.*/)
    if (!matches || !matches[1]) {
      throw new Error('cannot parse the IRI')
    }
    const entityName = matches[1]

    let model = models.find(candidate => { return candidate.entity === entityName })
    if (!model) {
      throw new Error('no model found')
    }
    return model
  }


}
