import type { DiscountLine, PromoScope } from './types.js'

export function lineMatchesScope(line: DiscountLine, scope: PromoScope): boolean {
  switch (scope.kind) {
    case 'all':
      return true
    case 'products':
      return line.productId !== undefined && scope.ids.includes(line.productId)
    case 'categories':
      return (
        line.categoryIds !== undefined &&
        line.categoryIds.some((id) => scope.ids.includes(id))
      )
    case 'tags':
      return line.tags !== undefined && line.tags.some((tag) => scope.tags.includes(tag))
    case 'vendor':
      return line.vendorId === scope.vendorId
  }
}

export function anyLineMatchesScope(lines: DiscountLine[], scope: PromoScope): boolean {
  return lines.some((line) => lineMatchesScope(line, scope))
}
