"use strict";
var __read = (this && this.__read)/* istanbul ignore next */ || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spread = (this && this.__spread)/* istanbul ignore next */ || function () {
    for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var errors = require("../../errors");
var StraightReplacementNodeHandler_1 = require("./StraightReplacementNodeHandler");
var NodeHandlerHelper_1 = require("./NodeHandlerHelper");
/**
 * Node handler for dealing with a parent who has a child that will change order.
 */
var ChangeChildOrderParentHandler = /** @class */ (function () {
    function ChangeChildOrderParentHandler(compilerFactory, opts) {
        this.compilerFactory = compilerFactory;
        this.straightReplacementNodeHandler = new StraightReplacementNodeHandler_1.StraightReplacementNodeHandler(compilerFactory);
        this.helper = new NodeHandlerHelper_1.NodeHandlerHelper(compilerFactory);
        this.oldIndex = opts.oldIndex;
        this.newIndex = opts.newIndex;
    }
    ChangeChildOrderParentHandler.prototype.handleNode = function (currentNode, newNode, newSourceFile) {
        var currentNodeChildren = this.getChildrenInNewOrder(currentNode.getCompilerChildren());
        var newNodeChildren = newNode.getChildren(newSourceFile);
        errors.throwIfNotEqual(newNodeChildren.length, currentNodeChildren.length, "New children length should match the old children length.");
        for (var i = 0; i < newNodeChildren.length; i++)
            this.helper.handleForValues(this.straightReplacementNodeHandler, currentNodeChildren[i], newNodeChildren[i], newSourceFile);
        this.compilerFactory.replaceCompilerNode(currentNode, newNode);
    };
    ChangeChildOrderParentHandler.prototype.getChildrenInNewOrder = function (children) {
        var result = __spread(children);
        var movingNode = result.splice(this.oldIndex, 1)[0];
        result.splice(this.newIndex, 0, movingNode);
        return result;
    };
    return ChangeChildOrderParentHandler;
}());
exports.ChangeChildOrderParentHandler = ChangeChildOrderParentHandler;
