"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenditionsConverter = void 0;
const rendition_1 = require("../contracts/base/rendition");
const invalid_operation_error_1 = require("../errors/invalid-operation-error");
class RenditionsConverter {
    /**
     * Serializes an array of renditions into a Json Value.
     *
     * @param value - An array of renditions
     *
     * @returns A Json Value.
     */
    serialize(renditions) {
        const result = {};
        renditions.forEach(rendition => {
            result[rendition.name] = rendition.items;
        });
        return JSON.stringify(result);
    }
    /**
     * Deserializes a Json Value into an array of renditions.
     *
     * @param value - A Json Value
     *
     * @returns An array with Renditions.
     */
    deserialize(value) {
        if (!value) {
            throw new invalid_operation_error_1.InvalidOperationError("Can't deserialize falsy value.");
        }
        else if (typeof value !== "object") {
            throw new invalid_operation_error_1.InvalidOperationError(`Expected a value of type 'JSON', but value was of type '${typeof value}'.`);
        }
        const renditions = typeof value === "string" ? JSON.parse(value) : JSON.parse(JSON.stringify(value));
        const result = [];
        Object.keys(renditions).forEach(renditionName => {
            const items = renditions[renditionName];
            result.push(rendition_1.Rendition.createRendition(renditionName, items));
        });
        return result;
    }
}
exports.RenditionsConverter = RenditionsConverter;
//# sourceMappingURL=renditions-converter.js.map