import qs from 'querystring';

import { getUrl } from '@blocklet/sdk/lib/component';
import { Router } from 'express';

const router = Router();

router.get('/checkout/:entryId', (req, res) => {
  const { entryId } = req.params;
  if (entryId.startsWith('plink_')) {
    return res.redirect(getUrl(`/checkout/pay/${entryId}?${qs.stringify(req.query as any)}`));
  }
  if (entryId.startsWith('prctbl_')) {
    return res.redirect(getUrl(`/checkout/pricing-table/${entryId}?${qs.stringify(req.query as any)}`));
  }

  return res.redirect(getUrl('/'));
});

export default router;
