import { TSchema, Data } from '@evolution-sdk/evolution';
import { MultisigSchema } from '../../types/multisig';

const InterestCollectionDatumSchema = TSchema.Struct({
  admin_permissions: MultisigSchema,
});

export type InterestCollectionDatum = typeof InterestCollectionDatumSchema.Type;

const InterestCollectionRedeemerSchema = TSchema.Union(
  TSchema.Literal('CollectInterest', { flatInUnion: true }),
  TSchema.Struct(
    {
      BatchCollectInterest: TSchema.Struct(
        {
          currentTime: TSchema.Integer,
          ownInputIndex: TSchema.Integer,
          collateralAssetRefInputIndex: TSchema.Integer,
          interestOracleRefInputIndex: TSchema.Integer,
        },
        { flatFields: true },
      ),
    },
    { flatInUnion: true },
  ),
  TSchema.Literal('Distribute', { flatInUnion: true }),
  TSchema.Literal('UpdatePermissions', { flatInUnion: true }),
  TSchema.Literal('UpgradeVersion', { flatInUnion: true }),
);

export type InterestCollectionRedeemer =
  typeof InterestCollectionRedeemerSchema.Type;

export function serialiseInterestCollectionRedeemer(
  redeemer: InterestCollectionRedeemer,
): string {
  return Data.withSchema(InterestCollectionRedeemerSchema).toCBORHex(redeemer);
}

export function parseInterestCollectionDatum(
  datum: string,
): InterestCollectionDatum {
  return Data.withSchema(InterestCollectionDatumSchema).fromCBORHex(datum);
}

export function serialiseInterestCollectionDatum(
  datum: InterestCollectionDatum,
): string {
  return Data.withSchema(InterestCollectionDatumSchema).toCBORHex(datum);
}
