am goes before (bi)labial sounds: p, f, b, m.
a’ goes before lenited ones: ph, bh, mh, ch, gh.
As for a’ – it doesn’t happen before dh and th because dental sounds (d, t) got delenited after n in Old Irish in(t) (→ modern an), so you have eg. an deoch the drink (instead of
a’ dheoch). And very early the article was int, and that final t still exists before lenited s as in an t-sràid the street and an t-sùil the eye (instead of a’ shràid or a’ shùil).