import { BaseMatcher } from "../BaseMatcher.js";
import { isArrayEqualStrict } from "../../collection/array/isArrayEqualStrict.js";

export class IsEqual extends BaseMatcher {
    #expected;

    constructor(expected) {
        super();
        this.#expected = expected;
    }

    matches(item, mismatch_description) {
        return this.areEqual(item, this.#expected);
    }

    describeTo(description) {
        description.appendValue(this.#expected);
    }


    areEqual(actual, expected) {
        if (expected === actual) {
            return true;
        }

        if (actual === null || actual === undefined) {
            return false;
        }

        if (expected !== null && Array.isArray(actual)) {
            return Array.isArray(expected) && isArrayEqualStrict(actual, expected);
        }

        if (typeof actual === "object" && typeof expected === "object" && typeof actual.equals === "function") {

            return actual.equals(expected);

        }

        return false;
    }


}
