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_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(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(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread 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 _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
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 { ConfigProvider } from "antd";
import classNames from "clsx";
import { AnimatePresence, motion } from "framer-motion";
import { isNumber, isObject, isString } from "lodash-es";
import toArray from "rc-util/lib/Children/toArray";
import React, { memo, useContext } from "react";
import { useTextAnimateStyle } from "./style";
var staggerTimings = {
    text: 0.06,
    word: 0.05,
    character: 0.03,
    line: 0.06,
    mix: 0.06
};
var defaultContainerVariants = {
    hidden: {
        opacity: 1
    },
    show: {
        opacity: 1,
        transition: {
            delayChildren: 0,
            staggerChildren: 0.05
        }
    },
    exit: {
        opacity: 0,
        transition: {
            staggerChildren: 0.05,
            staggerDirection: -1
        }
    }
};
var defaultItemVariants = {
    hidden: {
        opacity: 0
    },
    show: {
        opacity: 1
    },
    exit: {
        opacity: 0
    }
};
var defaultItemAnimationVariants = {
    fadeIn: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                opacity: 0,
                y: 20
            },
            show: {
                opacity: 1,
                y: 0,
                transition: {
                    duration: 0.3
                }
            },
            exit: {
                opacity: 0,
                y: 20,
                transition: {
                    duration: 0.3
                }
            }
        }
    },
    blurIn: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                opacity: 0,
                filter: 'blur(10px)'
            },
            show: {
                opacity: 1,
                filter: 'blur(0px)',
                transition: {
                    duration: 0.3
                }
            },
            exit: {
                opacity: 0,
                filter: 'blur(10px)',
                transition: {
                    duration: 0.3
                }
            }
        }
    },
    blurInUp: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                opacity: 0,
                filter: 'blur(10px)',
                y: 20
            },
            show: {
                opacity: 1,
                filter: 'blur(0px)',
                y: 0,
                transition: {
                    y: {
                        duration: 0.3
                    },
                    opacity: {
                        duration: 0.4
                    },
                    filter: {
                        duration: 0.3
                    }
                }
            },
            exit: {
                opacity: 0,
                filter: 'blur(10px)',
                y: 20,
                transition: {
                    y: {
                        duration: 0.3
                    },
                    opacity: {
                        duration: 0.4
                    },
                    filter: {
                        duration: 0.3
                    }
                }
            }
        }
    },
    blurInDown: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                opacity: 0,
                filter: 'blur(10px)',
                y: -20
            },
            show: {
                opacity: 1,
                filter: 'blur(0px)',
                y: 0,
                transition: {
                    y: {
                        duration: 0.3
                    },
                    opacity: {
                        duration: 0.4
                    },
                    filter: {
                        duration: 0.3
                    }
                }
            }
        }
    },
    slideUp: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                y: 20,
                opacity: 0
            },
            show: {
                y: 0,
                opacity: 1,
                transition: {
                    duration: 0.3
                }
            },
            exit: {
                y: -20,
                opacity: 0,
                transition: {
                    duration: 0.3
                }
            }
        }
    },
    slideDown: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                y: -20,
                opacity: 0
            },
            show: {
                y: 0,
                opacity: 1,
                transition: {
                    duration: 0.3
                }
            },
            exit: {
                y: 20,
                opacity: 0,
                transition: {
                    duration: 0.3
                }
            }
        }
    },
    slideLeft: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                x: 20,
                opacity: 0
            },
            show: {
                x: 0,
                opacity: 1,
                transition: {
                    duration: 0.3
                }
            },
            exit: {
                x: -20,
                opacity: 0,
                transition: {
                    duration: 0.3
                }
            }
        }
    },
    slideRight: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                x: -20,
                opacity: 0
            },
            show: {
                x: 0,
                opacity: 1,
                transition: {
                    duration: 0.3
                }
            },
            exit: {
                x: 20,
                opacity: 0,
                transition: {
                    duration: 0.3
                }
            }
        }
    },
    scaleUp: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                scale: 0.5,
                opacity: 0
            },
            show: {
                scale: 1,
                opacity: 1,
                transition: {
                    duration: 0.3,
                    scale: {
                        type: 'spring',
                        damping: 15,
                        stiffness: 300
                    }
                }
            },
            exit: {
                scale: 0.5,
                opacity: 0,
                transition: {
                    duration: 0.3
                }
            }
        }
    },
    scaleDown: {
        container: defaultContainerVariants,
        item: {
            hidden: {
                scale: 1.5,
                opacity: 0
            },
            show: {
                scale: 1,
                opacity: 1,
                transition: {
                    duration: 0.3,
                    scale: {
                        type: 'spring',
                        damping: 15,
                        stiffness: 300
                    }
                }
            },
            exit: {
                scale: 1.5,
                opacity: 0,
                transition: {
                    duration: 0.3
                }
            }
        }
    }
};
export var resolveSegments = function resolveSegments(children, by) {
    var result = [];
    toArray(children).forEach(function(item) {
        if (isString(item) || isNumber(item)) {
            var itemString = item.toString();
            if (itemString === '') return;
            switch(by){
                case 'word':
                    var _result;
                    (_result = result).push.apply(_result, _to_consumable_array(itemString.split(/(\s+)/)));
                    break;
                case 'character':
                    var _result1;
                    (_result1 = result).push.apply(_result1, _to_consumable_array(itemString.split('')));
                    break;
                case 'line':
                    var _result2;
                    (_result2 = result).push.apply(_result2, _to_consumable_array(itemString.split('\n')));
                    break;
                case 'text':
                default:
                    result.push(itemString);
            }
        } else {
            result.push(item);
        }
    });
    return result;
};
var TextAnimateBase = function TextAnimateBase(_0) {
    var children = _0.children, _0_delay = _0.delay, delay = _0_delay === void 0 ? 0 : _0_delay, _0_duration = _0.duration, duration = _0_duration === void 0 ? 0.3 : _0_duration, variants = _0.variants, className = _0.className, segmentClassName = _0.segmentClassName, tmp = _0.as, Component = tmp === void 0 ? 'p' : tmp, _0_startOnView = _0.startOnView, startOnView = _0_startOnView === void 0 ? true : _0_startOnView, _0_once = _0.once, once = _0_once === void 0 ? false : _0_once, _0_by = _0.by, by = _0_by === void 0 ? 'word' : _0_by, _0_animation = _0.animation, animation = _0_animation === void 0 ? 'fadeIn' : _0_animation, _0_accessible = _0.accessible, accessible = _0_accessible === void 0 ? true : _0_accessible, props = _object_without_properties(_0, [
        "children",
        "delay",
        "duration",
        "variants",
        "className",
        "segmentClassName",
        "as",
        "startOnView",
        "once",
        "by",
        "animation",
        "accessible"
    ]);
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var prefixCls = getPrefixCls('text-animate');
    var _useTextAnimateStyle = useTextAnimateStyle(prefixCls), wrapSSR = _useTextAnimateStyle.wrapSSR, hashId = _useTextAnimateStyle.hashId;
    var MotionComponent = motion(Component);
    var segments = resolveSegments(children, by);
    var staggerChildren = segments.length > 0 ? duration / segments.length : 0;
    var finalVariants = variants ? {
        container: {
            hidden: {
                opacity: 0
            },
            show: {
                opacity: 1,
                transition: {
                    opacity: {
                        duration: 0.01,
                        delay: delay
                    },
                    delayChildren: delay,
                    staggerChildren: staggerChildren
                }
            },
            exit: {
                opacity: 0,
                transition: {
                    staggerChildren: staggerChildren,
                    staggerDirection: -1
                }
            }
        },
        item: variants
    } : animation ? {
        container: _object_spread_props(_object_spread({}, defaultItemAnimationVariants[animation].container), {
            show: _object_spread_props(_object_spread({}, defaultItemAnimationVariants[animation].container.show), {
                transition: {
                    delayChildren: delay,
                    staggerChildren: staggerChildren
                }
            }),
            exit: _object_spread_props(_object_spread({}, defaultItemAnimationVariants[animation].container.exit), {
                transition: {
                    staggerChildren: staggerChildren,
                    staggerDirection: -1
                }
            })
        }),
        item: defaultItemAnimationVariants[animation].item
    } : {
        container: defaultContainerVariants,
        item: defaultItemVariants
    };
    return wrapSSR(/*#__PURE__*/ React.createElement(AnimatePresence, {
        mode: "popLayout"
    }, /*#__PURE__*/ React.createElement(MotionComponent, _object_spread({
        variants: finalVariants.container,
        initial: "hidden",
        whileInView: startOnView ? 'show' : undefined,
        animate: startOnView ? undefined : 'show',
        exit: "exit",
        className: classNames(prefixCls, hashId, className),
        viewport: {
            once: once
        },
        "aria-label": accessible && isString(children) ? children : undefined
    }, props), segments.map(function(segment, i) {
        return /*#__PURE__*/ React.createElement(motion.span, {
            key: "".concat(by, "-").concat(isObject(segment) ? segment.key : segment, "-").concat(i),
            variants: finalVariants.item,
            custom: i * staggerTimings[by],
            className: classNames("".concat(prefixCls, "-item"), "".concat(prefixCls, "-item-").concat(by), hashId, segmentClassName),
            "aria-hidden": accessible ? true : undefined
        }, segment);
    }))));
};
// Export the memoized version
export var TextAnimate = /*#__PURE__*/ memo(TextAnimateBase);
