import { DEFAULT_LICENSE } from './license.defaults';
import { mergeLicenseFeaturesWithPlan, mergeLicenseLimitsWithPlan } from './license.catalog';
import type { AsukaLicenseEntitlements } from './license.types';

export function normalizeLicense(
  input?: Partial<AsukaLicenseEntitlements> | null,
): AsukaLicenseEntitlements {
  const plan = (input?.plan ?? DEFAULT_LICENSE.plan) as AsukaLicenseEntitlements['plan'];
  return {
    ...DEFAULT_LICENSE,
    ...(input ?? {}),
    plan,
    features: mergeLicenseFeaturesWithPlan(plan, (input?.features ?? {}) as Record<string, boolean>),
    limits: mergeLicenseLimitsWithPlan(plan, input?.limits ?? {}),
    usage: {
      ...((input?.usage ?? {}) as Record<string, number | string | boolean | undefined>),
    },
  };
}
