import "@johnlindquist/kit";
import net from "node:net";
import { URL } from "node:url";
const getLookupData = async (query) => {
  
  const response = await get(
    `http://ip-api.com/json/${query}?fields=status,message,continent,country,countryCode,regionName,city,zip,lat,lon,timezone,isp,org,as,query`
  );
  if (response.data.status === "fail") {
    throw new Error(response.data.message);
  }
  return response.data;
};
let lookupQuery = await arg({
  placeholder: "Enter IP address or domain",
  validate: (value) => {
    if (net.isIP(value) !== 0) {
      return true;
    } else {
      try {
        new URL(`https://${value}`);
        return true;
      } catch (e) {
        return "Please enter a valid IP address or domain";
      }
    }
  },
});
const data = await getLookupData(lookupQuery);
div(
  md(`
# IP Lookup: ${lookupQuery}
- **IP:** ${data.query}
- **ISP:** ${data.isp}
- **Organization:** ${data.org}
- **AS:** ${data.as}
- **Continent:** ${data.continent}
- **Country:** ${data.country} (${data.countryCode})
- **Region:** ${data.regionName}
- **City:** ${data.city}
- **Zip Code:** ${data.zip}
- **Latitude:** ${data.lat}
- **Longitude:** ${data.lon}
- **Timezone:** ${data.timezone}
`)
);