import { applyParamsToScript } from '@lucid-evolution/lucid';
import { SpendingValidator } from '@lucid-evolution/lucid';
import { _interestCollectionValidator } from '../../validators/interest-collection-validator';
import {
  castInterestCollectionParams,
  InterestCollectionParams,
} from './types';
import {
  fromSystemParamsAssetLucid,
  InterestCollectionParamsSP,
} from '../../types/system-params';

export function mkInterestCollectionValidator(
  params: InterestCollectionParams,
): SpendingValidator {
  return {
    type: 'PlutusV3',
    script: applyParamsToScript(_interestCollectionValidator.cborHex, [
      castInterestCollectionParams(params),
    ]),
  };
}

export const mkInterestCollectionValidatorFromSP = (
  params: InterestCollectionParamsSP,
): SpendingValidator => {
  return {
    type: 'PlutusV3',
    script: applyParamsToScript(_interestCollectionValidator.cborHex, [
      castInterestCollectionParams({
        versionRecordNft: fromSystemParamsAssetLucid(params.versionRecordNft),
        multisigUtxoNft: fromSystemParamsAssetLucid(params.multisigUtxoNft),
        cdpAuthTk: fromSystemParamsAssetLucid(params.cdpAuthTk),
        collateralAssetAuthTk: fromSystemParamsAssetLucid(
          params.collateralAssetAuthTk,
        ),
        govAuthTk: fromSystemParamsAssetLucid(params.govAuthTk),
        cdpAssetSymbol: params.cdpAssetSymbol.unCurrencySymbol,
        cdpBiasTime: BigInt(params.cdpBiasTime),
        interestSettlementCooldown: BigInt(params.interestSettlementCooldown),
      }),
    ]),
  };
};
