/* *
 *
 *  (c) 2009-2026 Highsoft AS
 *
 *  Integration of this software requires a license.
 *  - For commercial use, see www.highcharts.com/license
 *  - For non-commercial, see www.highcharts.com/license-eula
 *
 *
 *  Authors:
 *  - Sophie Bremer
 *
 * */
'use strict';
import ChainModifier from '../../Data/Modifiers/ChainModifier.js';
import Serializable from '../Serializable.js';
/* *
 *
 *  Functions
 *
 * */
/**
 * Converts the given JSON to a class instance.
 *
 * @param {JSON} json
 * JSON to deserialize as a class instance or object.
 *
 * @return {ChainModifier}
 * Returns the class instance or object, or throws an exception.
 */
function fromJSON(json) {
    const chainOptions = json.options.chain, jsonChain = json.chain, modifiers = [];
    // Modifiers
    for (let i = 0, iEnd = jsonChain.length; i < iEnd; ++i) {
        modifiers.push(Serializable.fromJSON(jsonChain[i]));
    }
    // Apply chain options later
    delete json.options.chain;
    const chainModifier = new ChainModifier(json.options, ...modifiers);
    chainModifier.options.chain = chainOptions;
    // Done
    return chainModifier;
}
/**
 * Validates the given class instance for JSON support.
 *
 * @param {AnyRecord} obj
 * Class instance or object to validate.
 *
 * @return {boolean}
 * Returns true, if the function set can convert the given object, otherwise
 * false.
 */
function jsonSupportFor(obj) {
    return obj instanceof ChainModifier;
}
/**
 * Converts the given class instance to JSON.
 *
 * @param {ChainModifier} obj
 * Class instance or object to serialize as JSON.
 *
 * @return {JSON}
 * Returns the JSON of the class instance or object.
 */
function toJSON(obj) {
    const chain = [], options = obj.options;
    // Modifiers
    const objChain = obj.chain;
    for (let i = 0, iEnd = objChain.length; i < iEnd; ++i) {
        chain.push(Serializable.toJSON(objChain[i]));
    }
    // Done
    return {
        $class: 'Data.ChainModifier',
        chain: [],
        options
    };
}
/* *
 *
 *  Registry
 *
 * */
const ChainModifierHelper = {
    $class: 'Data.ChainModifier',
    fromJSON,
    jsonSupportFor,
    toJSON
};
Serializable.registerHelper(ChainModifierHelper);
/* *
 *
 *  Default Export
 *
 * */
export default ChainModifierHelper;
