"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getItemTitle = exports.getUrlOrigin = void 0;
exports.insert = insert;
exports.removeFromArray = removeFromArray;
exports.swapArrayItems = swapArrayItems;
exports.reorderArrayItems = reorderArrayItems;
exports.duplicateArrayItem = duplicateArrayItem;
exports.insertByPath = insertByPath;
exports.splitPathAndIndex = splitPathAndIndex;
exports.generateChildrenPathFromArray = generateChildrenPathFromArray;
exports.modifyObjectByPath = modifyObjectByPath;
exports.isItemsNeighbours = isItemsNeighbours;
exports.getDestinationShiftBeforeReorder = getDestinationShiftBeforeReorder;
exports.prepareShift = prepareShift;
const tslib_1 = require("tslib");
const lodash_1 = tslib_1.__importDefault(require("lodash"));
tslib_1.__exportStar(require("./code.js"), exports);
tslib_1.__exportStar(require("./cn.js"), exports);
function insert(arr, index, newItem) {
    return [...arr.slice(0, index), newItem, ...arr.slice(index)];
}
function removeFromArray(array, index) {
    return [...array.slice(0, index), ...array.slice(index + 1)];
}
function swapArrayItems(array, firstIndex, secondIndex) {
    const results = array.slice();
    const firstItem = array[firstIndex];
    results[firstIndex] = array[secondIndex];
    results[secondIndex] = firstItem;
    return results;
}
function reorderArrayItems(array, index, destination) {
    const min = Math.min(index, destination);
    const max = Math.max(index, destination);
    const firstOperationRemove = index < destination;
    const result = [];
    result.push(...array.slice(0, min));
    if (!firstOperationRemove) {
        result.push(array[index]);
    }
    result.push(...array.slice(firstOperationRemove ? min + 1 : min, max));
    if (firstOperationRemove) {
        result.push(array[index]);
    }
    result.push(...array.slice(firstOperationRemove ? max : max + 1, array.length));
    return result.filter((item) => item !== undefined);
}
function duplicateArrayItem(array, index) {
    const duplicatedItem = lodash_1.default.cloneDeep(array[index]);
    return [...array.slice(0, index), duplicatedItem, ...array.slice(index)];
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function insertByPath(object, path, value) {
    if (!path) {
        return value;
    }
    return lodash_1.default.setWith(lodash_1.default.clone(object), path, value, lodash_1.default.clone);
}
/*
 *  path: string;
 *  Example:
 *  1. blocks[0] => {path: blocks, index: 0}
 *  2. blocks[2].children[10] => {path: blocks[2].children, index: 10}
 **/
function splitPathAndIndex(path) {
    // Match blocks[3], blocks[0].children[12], blocks[0], blocks[999999]
    const bracketsRegExp = /(.*)\[(\d+)]$/g;
    const regexpMatches = Array.from(path.matchAll(bracketsRegExp));
    if (regexpMatches.length) {
        return {
            // blocks, blocks[0].children
            path: regexpMatches[0][1],
            // 3, 12, 0, 9999
            index: Number(regexpMatches[0][2]),
        };
    }
    // eslint-disable-next-line no-console
    console.error('Non correct path for splitting');
    return undefined;
}
/*
 * [0, 4, 3] => [0].children[4].children[3]
 * */
function generateChildrenPathFromArray(indexes) {
    if (!indexes.length) {
        return '';
    }
    let resultPath = `[${indexes[0]}]`;
    if (indexes.length > 1) {
        for (let i = 1; i < indexes.length; i++) {
            resultPath += `.children[${indexes[i]}]`;
        }
    }
    return resultPath;
}
function modifyObjectByPath(blocks, arrayPath, modifyCallback) {
    // [1]
    // [4].children[3]
    const insertPath = generateChildrenPathFromArray(arrayPath);
    // path: ''                 index: 1
    // path: '[4].children'     index: 3
    const splitPath = splitPathAndIndex(insertPath);
    if (splitPath) {
        const { path: parentPath, index } = splitPath;
        // Get Array that lies on path
        const parentArray = parentPath ? lodash_1.default.get(blocks, parentPath) : blocks;
        const value = Array.isArray(parentArray) ? parentArray : [];
        // Modify Array
        const newModifiedArray = modifyCallback(value, index);
        // Return it back
        return insertByPath(blocks, parentPath, newModifiedArray);
    }
    return blocks;
}
function isItemsNeighbours(arrayA, arrayB) {
    if (arrayA.length !== arrayB.length) {
        return false;
    }
    for (let i = 0; i < arrayA.length - 1; i++) {
        if (arrayA[i] !== arrayB[i]) {
            return false;
        }
    }
    return true;
}
function getDestinationShiftBeforeReorder(arrayInit, arrayDest) {
    if (arrayInit.length === arrayDest.length || arrayInit.length > arrayDest.length) {
        return arrayDest;
    }
    for (let i = 0; i < arrayInit.length; i++) {
        if (arrayInit[i] < arrayDest[i]) {
            return prepareShift(arrayInit, arrayDest);
        }
    }
    return arrayDest;
}
function prepareShift(arrayInit, arrayDest) {
    if (arrayInit.length === arrayDest.length || arrayInit.length > arrayDest.length) {
        return arrayDest;
    }
    return arrayDest.map((pathIndex, index) => index === arrayInit.length - 1 ? pathIndex - 1 : pathIndex);
}
const getUrlOrigin = (url) => {
    try {
        const urlObject = new URL(url);
        return urlObject.origin;
    }
    catch {
        return undefined;
    }
};
exports.getUrlOrigin = getUrlOrigin;
const getItemTitle = (item) => {
    return (lodash_1.default.get(item, 'title.text') ||
        lodash_1.default.get(item, 'title') ||
        lodash_1.default.get(item, 'textContent.title') ||
        lodash_1.default.get(item, 'content.title'));
};
exports.getItemTitle = getItemTitle;
//# sourceMappingURL=index.js.map
