function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { useEffect, useState } from "react";
import { StyledTextSkeleton } from "./TextSkeleton.styles";
var variousWidth = [
    7.58,
    5.27,
    13.54,
    6.63,
    0.28,
    14.8,
    0.33,
    11.26,
    14.1,
    10.59,
    3.38,
    13.5,
    7.71,
    3.34,
    7.96
];
/**
 * Хок для создания компонента плейсхолдера нескольких строк текста.
 * Размеры компонента задаются с помощью констант и соответствуют размерам [типографических элементов](/?path=/docs/).
 */ export var textSkeleton = function textSkeleton(Component) {
    return function(_0) {
        var lines = _0.lines, width = _0.width, roundness = _0.roundness, customGradientColor = _0.customGradientColor, lighter = _0.lighter, _0_animationType = _0.animationType, animationType = _0_animationType === void 0 ? 'shimmer' : _0_animationType, customFadeInColor = _0.customFadeInColor, customFadeOutColor = _0.customFadeOutColor, _0_size = _0.size, size = _0_size === void 0 ? 'bodyM' : _0_size, _0_view = _0.view, view = _0_view === void 0 ? 'default' : _0_view, props = _object_without_properties(_0, [
            "lines",
            "width",
            "roundness",
            "customGradientColor",
            "lighter",
            "animationType",
            "customFadeInColor",
            "customFadeOutColor",
            "size",
            "view"
        ]);
        var _useState = _sliced_to_array(useState(null), 2), fixedWidth = _useState[0], setFixedWidth = _useState[1];
        var _useState1 = _sliced_to_array(useState([]), 2), linesWidth = _useState1[0], setLinesWidth = _useState1[1];
        useEffect(function() {
            if (!width) {
                setFixedWidth(null);
                var computedLengths = Array(lines).fill(0).reduce(function(acc, _, i) {
                    var w;
                    // Последняя строка
                    if (i === lines - 1 && lines !== 1) {
                        w = 45 - variousWidth[i % variousWidth.length];
                    } else if (lines === 1 || lines === 2) {
                        // Единственная или первая из двух
                        w = 100;
                    } else {
                        w = 100 - variousWidth[i % variousWidth.length];
                    }
                    return _object_spread_props(_object_spread({}, acc), _define_property({}, i, w));
                }, {});
                setLinesWidth(Object.values(computedLengths));
                return;
            }
            if (!Number.isNaN(Number(width))) {
                setFixedWidth("".concat(width, "%"));
            } else {
                setFixedWidth(String(width));
            }
        }, [
            width,
            lines
        ]);
        // TODO: Подумать как можно обойти явное приведение типов
        var ComponentSkeleton = Component;
        return /*#__PURE__*/ React.createElement(StyledTextSkeleton, props, Array.from(Array(lines), function(_, i) {
            return /*#__PURE__*/ React.createElement(ComponentSkeleton, {
                key: "line:".concat(i),
                size: size,
                view: view,
                roundness: roundness,
                animationType: animationType,
                lighter: lighter,
                customGradientColor: customGradientColor,
                customFadeInColor: customFadeInColor,
                customFadeOutColor: customFadeOutColor,
                style: {
                    width: fixedWidth || "".concat(linesWidth[i], "%")
                }
            });
        }));
    };
};
