import { TaJson } from "ta-json";
import { InvalidOperationError } from "../errors/invalid-operation-error";
import Link from "../link";
import { RelationResource } from "../models/relation-resource";
export class RelationResourceConverter {
    /**
     * Serializes a RelationResource or a Link into a JsonValue.
     *
     * @param value - A RelationResource or a Link
     *
     * @returns A Json Value.
     */
    serialize(value) {
        return TaJson.serialize(value);
    }
    /**
     * Deserializes a JsonValue into a RelationResource or a Link.
     *
     * @param value - A Json Value
     *
     * @returns A RelationResource or a Link.
     */
    deserialize(value) {
        if (!value) {
            throw new InvalidOperationError("Can't deserialize falsy value.");
        }
        else if (typeof value !== "object" || value instanceof Array) {
            throw new InvalidOperationError(`Expected a value of type 'JSON', but value was of type '${typeof value}'.`);
        }
        const obj = typeof value === "string" ? JSON.parse(value) : JSON.parse(JSON.stringify(value));
        if (obj.href) {
            return TaJson.deserialize(obj, Link);
        }
        else if (this.isRelationResource(obj)) {
            return TaJson.deserialize(value, RelationResource);
        }
        else {
            throw new InvalidOperationError("Not a known format");
        }
    }
    isRelationResource(arg) {
        return arg.self !== undefined;
    }
}
//# sourceMappingURL=relation-resource-converter.js.map