Implement apply_merge_patch(document, patch) using RFC 7396-style semantics.
If patch is not a mapping, it replaces the entire document. For a mapping patch, start from the document when it is a mapping or from an empty mapping otherwise. A null value deletes that key; other values apply recursively. Arrays are values and are replaced, not merged by index. Return a deep-independent result and never mutate or retain mutable aliases to either input.