/* See JOSM #22822 - runway heading validator by gaben */

/* raising error only if {first designator >= the second designator} */
way[aeroway=runway][ref =~ /^([0-9]{1,3}[LCRWSGHU]? ?[-,;\/\\] ?[0-9]{1,3}[LCRWSGHU]?)$/][eval(to_int(get(regexp_match("^([0-9]+)[LCRWSGHU]? ?[-,;\\/] ?([0-9]+)[LCRWSGHU]?$", tag("ref")), 1)) >= to_int(get(regexp_match("^([0-9]+)[LCRWSGHU]? ?[-,;\\/] ?([0-9]+)[LCRWSGHU]?$", tag("ref")), 2)))] {
  group: tr("runway reference");
  set runwayDesignatorOrderIssue;
  /* throwError: tr("designators should be in increasing order"); add this line to pass tests... I think it's a bug */
  assertMatch: "way aeroway=runway ref=02-01";
  assertMatch: "way aeroway=runway ref=\"2- 1\"";
  assertMatch: "way aeroway=runway ref=\"2 - 1\"";
  assertMatch: "way aeroway=runway ref=36/18";
  /* assertMatch: "way aeroway=runway ref=\"36\18\""; not working yet :( */
  assertMatch: "way aeroway=runway ref=36W-17W";
  assertMatch: "way aeroway=runway ref=01/01";
  assertNoMatch: "way aeroway=runway ref=17W-36W";
  assertNoMatch: "way aeroway=runway ref=01- 02";
  assertNoMatch: "way aeroway=runway ref=03H/12H";
  assertNoMatch: "way aeroway=runway ref=01-19";
  assertNoMatch: "way aeroway=runway ref=3/21";
  assertNoMatch: "way aeroway=runway ref=09L/27R";
}

/* raising error only if {second designator - first designator != 18} [global version] */
way[aeroway=runway][ref =~ /^([0-9]{1,2}[LCRWSGHU]? ?[-,;\/\\] ?[0-9]{1,2}[LCRWSGHU]?)$/][eval(to_int(get(regexp_match("^([0-9]+)[LCRWSGHU]? ?[-,;\\/] ?([0-9]+)[LCRWSGHU]?$", tag("ref")), 2)) - to_int(get(regexp_match("^([0-9]+)[LCRWSGHU]? ?[-,;\\/] ?([0-9]+)[LCRWSGHU]?$", tag("ref")), 1)) != 18)]!.runwayDesignatorOrderIssue {
  group: tr("runway reference");
  set global180RuleIssue;
  throwError: tr("numbers don''t follow the 180 degree rule (global version)");
  assertMatch: "way aeroway=runway ref=\"01 -02R\"";
  assertMatch: "way aeroway=runway ref=17W-36W";
  assertMatch: "way aeroway=runway ref=\"01/ 01\"";
  assertMatch: "way aeroway=runway ref=03H/12H";
  assertNoMatch: "way aeroway=runway ref=01-19";
  assertNoMatch: "way aeroway=runway ref=\"3 / 21\"";
  assertNoMatch: "way aeroway=runway ref=09L/27R";
}

/* raising error only if {second designator - first designator != 180} [American version] */
way[aeroway=runway][ref =~ /^([0-9]{1,3}[LCRWSGHU]? ?[-,;\/\\] ?[0-9]{3}[LCRWSGHU]?)$/][eval(to_int(get(regexp_match("^([0-9]+)[LCRWSGHU]? ?[-,;\\/] ?([0-9]+)[LCRWSGHU]?$", tag("ref")), 2)) - to_int(get(regexp_match("^([0-9]+)[LCRWSGHU]? ?[-,;\\/] ?([0-9]+)[LCRWSGHU]?$", tag("ref")), 1)) != 180)]!.runwayDesignatorOrderIssue {
  group: tr("runway reference");
  set american180RuleIssue;
  throwError: tr("numbers don''t follow the 180 degree rule (American version)");
  assertMatch: "way aeroway=runway ref=170W-360W";
  assertMatch: "way aeroway=runway ref=\"10/ 100\"";
  assertMatch: "way aeroway=runway ref=30H/120H";
  assertNoMatch: "way aeroway=runway ref=10-190";
  assertNoMatch: "way aeroway=runway ref=\"30 / 210\"";
  assertNoMatch: "way aeroway=runway ref=09L/27R";
}

/* offer heading separator fix */
way[aeroway=runway][ref =~ /^([0-9]{1,3}[LCRWSGHU]? [-,;\/\\][0-9]{1,3}[LCRWSGHU]?)$/]!.runwayDesignatorOrderIssue!.global180RuleIssue!.american180RuleIssue,
way[aeroway=runway][ref =~ /^([0-9]{1,3}[LCRWSGHU]?[-,;\/\\] [0-9]{1,3}[LCRWSGHU]?)$/]!.runwayDesignatorOrderIssue!.global180RuleIssue!.american180RuleIssue,
way[aeroway=runway][ref =~ /^([0-9]{1,3}[LCRWSGHU]? [-,;\/\\] [0-9]{1,3}[LCRWSGHU]?)$/]!.runwayDesignatorOrderIssue!.global180RuleIssue!.american180RuleIssue {
  group: tr("runway reference");
  assertMatch: "way aeroway=runway ref=\"09L /27R\"";
  assertMatch: "way aeroway=runway ref=\"09L/ 27R\"";
  assertNoMatch: "way aeroway=runway ref=09L/27R";
  throwWarning: tr("values can be value can be prettified, separator");
  fixAdd: concat("ref=", eval(get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1) > get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2)) ? get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2) : get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1) , "/",
  eval(get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1) > get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2)) ? get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1) : get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2));
}

/* offer heading order fix */
way[aeroway=runway].runwayDesignatorOrderIssue!.global180RuleIssue!.american180RuleIssue {
  group: tr("runway reference");
  throwError: tr("designators should be in increasing order");
  fixAdd: concat("ref=", eval(get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1) > get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2)) ? get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2) : get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1) , "/",
    eval(get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1) > get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2)) ? get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1) : get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2));
}

/* offer heading leading zero fix (except USA, Mexico) */
way[aeroway=runway][outside("US,MX")][ref =~ /^([0-9]{1}[LCRWSGHU]? ?[-,;\/\\] ?[0-9]{1,3}[LCRWSGHU]?)$/]!.runwayDesignatorOrderIssue!.global180RuleIssue!.american180RuleIssue {
  group: tr("runway reference");
  assertMatch: "way aeroway=runway ref=9L/27R";
  assertNoMatch: "way aeroway=runway ref=09L/27R";
  throwWarning: tr("values can be value can be prettified, leading zero");
  fixAdd: concat("ref=0", get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 1), "/", get(regexp_match("^([0-9]+[LCRWSGHU]?) ?[-,;\\/] ?([0-9]+[LCRWSGHU]?)$", tag("ref")), 2));
}