/**
 * Copyright 2015 CANAL+ Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import log from "../log";
import arrayFindIndex from "../utils/array_find_index";
import { MANIFEST_UPDATE_TYPE } from "./types";
/**
 * Update oldPeriod attributes with the one from newPeriod (e.g. when updating
 * the Manifest).
 * @param {Object} oldPeriod
 * @param {Object} newPeriod
 * @param {number} updateType
 * @returns {Object}
 */
export default function updatePeriodInPlace(oldPeriod, newPeriod, updateType) {
    var res = {
        updatedAdaptations: [],
        removedAdaptations: [],
        addedAdaptations: [],
    };
    oldPeriod.start = newPeriod.start;
    oldPeriod.end = newPeriod.end;
    oldPeriod.duration = newPeriod.duration;
    oldPeriod.streamEvents = newPeriod.streamEvents;
    var oldAdaptations = oldPeriod.getAdaptations();
    var newAdaptations = newPeriod.getAdaptations();
    var _loop_1 = function (j) {
        var _a;
        var oldAdaptation = oldAdaptations[j];
        var newAdaptationIdx = arrayFindIndex(newAdaptations, function (a) { return a.id === oldAdaptation.id; });
        if (newAdaptationIdx === -1) {
            log.warn("Manifest: Adaptation \"" +
                oldAdaptations[j].id +
                "\" not found when merging.");
            var removed = oldAdaptations.splice(j, 1)[0];
            j--;
            res.removedAdaptations.push(removed);
        }
        else {
            var newAdaptation = newAdaptations.splice(newAdaptationIdx, 1)[0];
            var updatedRepresentations = [];
            var addedRepresentations = [];
            var removedRepresentations = [];
            res.updatedAdaptations.push({ adaptation: oldAdaptation, updatedRepresentations: updatedRepresentations, addedRepresentations: addedRepresentations, removedRepresentations: removedRepresentations });
            var oldRepresentations = oldAdaptation.representations;
            var newRepresentations = newAdaptation.representations.slice();
            var _loop_2 = function (k) {
                var oldRepresentation = oldRepresentations[k];
                var newRepresentationIdx = arrayFindIndex(newRepresentations, function (representation) {
                    return representation.id === oldRepresentation.id;
                });
                if (newRepresentationIdx === -1) {
                    log.warn("Manifest: Representation \"".concat(oldRepresentations[k].id, "\" ") +
                        "not found when merging.");
                    var removed = oldRepresentations.splice(k, 1)[0];
                    k--;
                    removedRepresentations.push(removed);
                }
                else {
                    var newRepresentation = newRepresentations.splice(newRepresentationIdx, 1)[0];
                    updatedRepresentations.push(oldRepresentation);
                    oldRepresentation.cdnMetadata = newRepresentation.cdnMetadata;
                    if (updateType === MANIFEST_UPDATE_TYPE.Full) {
                        oldRepresentation.index._replace(newRepresentation.index);
                    }
                    else {
                        oldRepresentation.index._update(newRepresentation.index);
                    }
                }
                out_k_1 = k;
            };
            var out_k_1;
            for (var k = 0; k < oldRepresentations.length; k++) {
                _loop_2(k);
                k = out_k_1;
            }
            if (newRepresentations.length > 0) {
                log.warn("Manifest: ".concat(newRepresentations.length, " new Representations ") +
                    "found when merging.");
                (_a = oldAdaptation.representations).push.apply(_a, newRepresentations);
                addedRepresentations.push.apply(addedRepresentations, newRepresentations);
            }
        }
        out_j_1 = j;
    };
    var out_j_1;
    for (var j = 0; j < oldAdaptations.length; j++) {
        _loop_1(j);
        j = out_j_1;
    }
    if (newAdaptations.length > 0) {
        log.warn("Manifest: ".concat(newAdaptations.length, " new Adaptations ") +
            "found when merging.");
        for (var _i = 0, newAdaptations_1 = newAdaptations; _i < newAdaptations_1.length; _i++) {
            var adap = newAdaptations_1[_i];
            var prevAdaps = oldPeriod.adaptations[adap.type];
            if (prevAdaps === undefined) {
                oldPeriod.adaptations[adap.type] = [adap];
            }
            else {
                prevAdaps.push(adap);
            }
            res.addedAdaptations.push(adap);
        }
    }
    return res;
}
