[ty] Refactor "desperate" import resolution to use candidate operations that will also apply to module enumeration. (#28543)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:
- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
requests.)
- Does this pull request include references to any relevant issues?
- Does this PR follow our AI policy
(https://github.com/astral-sh/.github/blob/main/AI_POLICY.md)?
-->
## Summary
<!-- What's the purpose of the change? What does it do, and why? -->
This is a small step towards the [larger
refactor](https://github.com/astral-sh/ruff/pull/28199) that will allow
us to share ordinary module resolution rules with the module enumeration
process required to [support namespace packages for auto
imports](https://github.com/astral-sh/ty/issues/2273) (please see that
refactor PR for a detailed description of our motivations). It refactors
the `desperately_resolve_name` method (used for ordinary module
resolution) to use methods of `NameResolver` that can be shared with
module enumeration.
## Test Plan
This is a pure refactor that relies on existing tests.
<!-- How was it tested? -->