// Copyright 2017-2026 @pezkuwi/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { BlockNumber } from '@pezkuwi/types/interfaces';
import type { LeasePeriod } from './types.js';

import { useMemo } from 'react';

import { createNamedHook, useApi, useBestNumber } from '@pezkuwi/react-hooks';
import { BN_ZERO } from '@pezkuwi/util';

function useLeasePeriodImpl (): LeasePeriod | undefined {
  const { api } = useApi();
  const bestNumber = useBestNumber();

  return useMemo((): LeasePeriod | undefined => {
    if (!api.consts.slots.leasePeriod || !bestNumber) {
      return;
    }

    const length = api.consts.slots.leasePeriod as BlockNumber;
    const startNumber = bestNumber.sub((api.consts.slots.leaseOffset as BlockNumber) || BN_ZERO);
    const progress = startNumber.mod(length);

    return {
      currentPeriod: startNumber.div(length),
      length,
      progress,
      remainder: length.sub(progress)
    };
  }, [api, bestNumber]);
}

export default createNamedHook('useLeasePeriod', useLeasePeriodImpl);
