import {
  DEMAND_ACCOUNT_PAGE_PATHNAME,
  DEMAND_ADVERTISER_PAGE_PATHNAME,
  DEMAND_TAG_PAGE_PATHNAME,
  SUPPLY_ACCOUNT_PAGE_PATHNAME,
  SUPPLY_SITE_PAGE_PATHNAME,
  SUPPLY_TAG_PAGE_PATHNAME,
} from '../../account/constants';

const addDuplicateQueryIfExists = (link, isDuplicate) => (isDuplicate ? `${link}&duplicate` : link);

export const getSupplyTagLink = (tag, isDuplicate) => {
  if (!tag.accountId || !tag.siteId) {
    return null;
  }

  const link = `${SUPPLY_ACCOUNT_PAGE_PATHNAME}/${tag.accountId}${SUPPLY_SITE_PAGE_PATHNAME}/${tag.siteId}${SUPPLY_TAG_PAGE_PATHNAME}/${tag.id}?tagName=${encodeURIComponent(tag.name)}&accountName=${encodeURIComponent(tag.accountName ?? '')}&siteName=${encodeURIComponent(tag.siteName ?? '')}`;
  return addDuplicateQueryIfExists(link, isDuplicate);
};

export const getDemandTagLink = (tag, isDuplicate) => {
  if (!tag.daccountId || !tag.advertiserId) {
    return null;
  }

  const link = `${DEMAND_ACCOUNT_PAGE_PATHNAME}/${tag.daccountId}${DEMAND_ADVERTISER_PAGE_PATHNAME}/${tag.advertiserId}${DEMAND_TAG_PAGE_PATHNAME}/${tag.id}?tagName=${encodeURIComponent(tag.name ?? '')}&accountName=${encodeURIComponent(tag.accountName ?? '')}&advertiserName=${encodeURIComponent(tag.advertiserName ?? '')}`;
  return addDuplicateQueryIfExists(link, isDuplicate);
};

export const getSiteLink = (info) => {
  if (!info.accountId) {
    return null;
  }

  return `${SUPPLY_ACCOUNT_PAGE_PATHNAME}/${info.accountId}${SUPPLY_SITE_PAGE_PATHNAME}/${info.id}?accountName=${encodeURIComponent(info.accountName ?? '')}&siteName=${encodeURIComponent(info.name ?? '')}`;
};

export const getAdvertiserLink = (info) => {
  if (!info.daccountId) {
    return null;
  }

  return `${DEMAND_ACCOUNT_PAGE_PATHNAME}/${info.daccountId}${DEMAND_ADVERTISER_PAGE_PATHNAME}/${info.id}?accountName=${encodeURIComponent(info.accountName ?? '')}&advertiserName=${encodeURIComponent(info.name ?? '')}`;
};
