/** @module src/pntToArea */
import pkg from 'point-in-polygon'
const inside = pkg
const polyGb = [[-5.079346,54.356181],[-5.218856,54.352538],[-5.238015,54.577735],[-5.594707,55.067578],[-5.85957,55.233056],[-6.1989,55.442175],[-6.754907,55.520643],[-6.826016,56.098271],[-8.4242,56.025199],[-8.577402,56.917818],[-8.739734,57.810026],[-8.911963,58.7018],[-7.193942,58.782764],[-7.332053,59.676983],[-5.560279,59.737099],[-3.782533,59.77326],[-3.831945,60.670683],[-2.001839,60.683214],[-2.0019,61.58096],[-0.119146,61.568018],[-0.17173,60.67074],[-0.22103,59.773315],[-0.267328,58.875744],[-2.00173,58.887355],[-2.00168,57.989238],[-0.310874,57.978026],[-0.351892,57.080161],[-2.001633,57.090993],[-2.001588,56.192619],[-0.390579,56.182148],[-0.427115,55.283988],[1.145328,55.253591],[1.076342,54.356271],[1.011037,53.458761],[2.513016,53.411393],[2.420466,52.515203],[2.332666,51.618767],[2.249301,50.722093],[0.834724,50.76511],[0.781771,49.866861],[-0.609132,49.891897],[-2.001337,49.900236],[-3.39354,49.891859],[-4.784439,49.866784],[-6.172738,49.825069],[-7.55716,49.766807],[-7.662447,50.661841],[-6.252021,50.721974],[-6.33545,51.618645],[-5.822792,52.024604],[-5.051366,52.558456],[-5.051366,52.558457],[-4.952011,52.560978],[-5.013966,53.458674],[-5.079346,54.356181]]
const polyIr = [[-10.863443,51.218565],[-10.920638,52.116175],[-10.980909,53.013601],[-11.044479,53.91084],[-11.111595,54.80789],[-11.182531,55.704747],[-9.592679,55.735662],[-8.000759,55.74598],[-6.781597,55.739941],[-6.754907,55.520643],[-6.419264,55.473617],[-6.1989,55.442175],[-5.85957,55.233056],[-5.594707,55.067578],[-5.413363,54.820209],[-5.238015,54.577735],[-5.218856,54.352538],[-5.079346,54.356181],[-5.046624,53.9131],[-5.013966,53.458674],[-5.001631,53.283731],[-5.020559,53.013619],[-5.051336,52.558457],[-5.051366,52.558457],[-5.051366,52.558456],[-5.673366,52.129388],[-5.822792,52.024604],[-6.33545,51.618645],[-6.299809,51.241244],[-6.568641,51.244838],[-8.000721,51.253594],[-9.432801,51.24483],[-10.863443,51.218565]]
const polyCi = [[-1.614831,49.644257],[-1.639721,48.744984],[-3,48.753013],[-3,49.652543],[-3,49.895922],[-2.001337,49.900236],[-1.607534,49.899573],[-1.614831,49.644257]]
/**
* Given a WGS 84 lon, lat pair, return the two letter code corresponding to the area
* that the point is in. The three polygons describe the regions covered by the 100 km
* squares for the British, Irish and Channel Island coordinate systems. These areas are
* exclusive. Where they overlap, e.g. British and Irish, they have been divided by a line
* through the Irish see roughly midway between the land masses. For the Channel Islands
* and Britain, the CI UTM 30 grid is truncated where it overlaps the British grid. The
* function returns the two-letter code corresponding to the area: gb, ir or ci.
* It returns null if the point doesn't fall within any of these areas.
* @param {number} lon - Longitude.
* @param {number} lat - Latitude.
* @returns {string} - Two-letter code corresponding to the area, or null.
*/
export function pntToArea (lon, lat) {
if (inside([ lon, lat ], polyGb)) return 'gb'
if (inside([ lon, lat ], polyIr)) return 'ir'
if (inside([ lon, lat ], polyCi)) return 'ci'
return null
}