// src/utils/ipUpdate.ts



/**
 * 将 "startIp/number" 格式拆分为 start 和 end IP
 * @param ipWithRange 例如 "192.168.1.1/100"
 * @returns { start: string, end: string }
 */
 const parseIpRange = (ipWithRange: string): { start: string; end: string } => {
  if (!ipWithRange.includes('/')) {
    // 如果没有 /，说明是普通 IP
    return { start: ipWithRange, end: '' };
  }

  const [startIp, numberStr] = ipWithRange.split('/');
  const baseIpParts = startIp.split('.');
  const prefix = baseIpParts.slice(0, 3).join('.'); // 192.168.1
  const startLastOctet = parseInt(baseIpParts[3], 10); // 1
  const endLastOctet = parseInt(numberStr, 10); // 100

  return {
    start: `${prefix}.${Math.min(startLastOctet, endLastOctet)}`,
    end: `${prefix}.${Math.max(startLastOctet, endLastOctet)}`,
  };
};
//将ip分开由end和start组成
const ipSplicing = (startIp: string, endIp: string | null | undefined): string => {
  if (!endIp || endIp.trim() === '' || startIp === endIp) {
    return startIp;
  }

  const startParts = startIp.split('.');
  const endParts = endIp.split('.');
  const lastPartOfEndIp = endParts[endParts.length - 1];

  return `${startIp}/${lastPartOfEndIp}`;
};

export { ipSplicing, parseIpRange };