import lodash from 'lodash';

const checkIsUuid = (value) => {
  const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
  return typeof value === 'string' && uuidRegex.test(value);
};

const validatorRouter = {
  paramIdNumber: (to, from, next) => {
    const toId = to.params.id;
    if (toId && lodash.isInteger(lodash.toNumber(toId))) {
      return next();
    }

    return next('/404');
  },
  paramIdString: (to, from, next) => {
    const toId = to.params.id;
    if (toId && lodash.isString(toId)) {
      return next();
    }
    return next('/404');
  },
  paramUuid: (to, from, next) => {
    const id = to.params.id;
    if (id && lodash.isString(id) && checkIsUuid(id)) {
      return next();
    }

    return next('/404');
  },
  shopeeProductShopId: (to, from, next) => {
    const { productId, shopId } = to.query;

    if (productId && shopId && lodash.isInteger(lodash.toNumber(productId)) && lodash.isInteger(lodash.toNumber(shopId))) {
      return next();
    }

    return next('/404');
  },
  printerPdf: (to, from, next) => {
    const query = to.query;

    const queryFrom = query.from;

    if (queryFrom === 'shopee') {
      if (!(query.shopId && query.jobId)) {
        return next('/404');
      }
    }

    return next();
  },
};

export default validatorRouter;
