import { isCustomMethodIdentifier, isResourceCollectionIdentifier, isSingletonResource } from './resourceEvaluation.js';

/**
 * Checks whether the get method at a given path is valid
 *
 * @param resourcePath the resource path to inspect
 * @param resourcePathItems the resource paths generated by getResourcePathItems
 * @returns true if the resourcePath has an invalid get method, false otherwise
 */
export function isInvalidGetMethod(resourcePath, resourcePathItems) {
  return (
    !lastIdentifierIsPathParam(resourcePath) &&
    !(isResourceCollectionIdentifier(resourcePath) && isSingletonResource(resourcePathItems))
  );
}

/**
 * Checks whether the list method at a given path is valid
 *
 * @param resourcePath the resource path to inspect
 * @param resourcePaths the resource paths generated by getResourcePathItems
 * @returns true if the resourcePath has an invalid list method, false otherwise
 */
export function isInvalidListMethod(resourcePath, resourcePaths) {
  return (
    lastIdentifierIsPathParam(resourcePath) ||
    isSingletonResource(resourcePaths) ||
    isCustomMethodIdentifier(resourcePath)
  );
}

function lastIdentifierIsPathParam(resourceIdentifier) {
  if (resourceIdentifier.includes('.')) {
    resourceIdentifier = resourceIdentifier.substring(0, resourceIdentifier.lastIndexOf('.'));
  }
  return resourceIdentifier.at(-1) === '}';
}
