"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactNative = require("react-native");

var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated"));

var _chevron = _interopRequireDefault(require("../chevron"));

var _styles = require("./styles");

var _hooks = require("../hooks");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const AnimatedAccordion = ({
  isArrow = true,
  sizeIcon = 16,
  disabled = false,
  colorIcon = '#16182b',
  initExpand = false,
  handleIcon,
  styleChevron,
  contentHeight = 0,
  renderContent,
  otherProperty,
  onChangeState,
  styleTouchable,
  configExpanded,
  styleContainer,
  configCollapsed,
  isStatusFetching = false,
  isUnmountedContent = false,
  activeBackgroundIcon = '#e5f6ff',
  handleCustomTouchable,
  onAnimatedEndExpanded,
  onAnimatedEndCollapsed,
  handleContentTouchable,
  inactiveBackgroundIcon = '#fff0e4',
  handleIndicatorFetching
}) => {
  const [layout, onLayout] = (0, _hooks.useLayout)(0);
  const open = (0, _reactNativeReanimated.useSharedValue)(initExpand);
  const size = (0, _reactNativeReanimated.useSharedValue)(contentHeight);
  const [isUnmounted, setUnmountedContent] = (0, _react.useState)(isUnmountedContent);
  (0, _react.useEffect)(() => {
    (0, _reactNativeReanimated.runOnUI)(() => {
      'worklet';

      if (initExpand && layout) {
        size.value = layout.height;
      }
    })();
  }, [initExpand, layout, layout.height, size]);
  (0, _react.useEffect)(() => {
    (0, _reactNativeReanimated.runOnUI)(() => {
      'worklet';

      if (!isStatusFetching && layout) {
        size.value = layout.height;
      }
    })();
  }, [isStatusFetching, layout, size]);
  const progress = (0, _reactNativeReanimated.useDerivedValue)(() => open.value ? (0, _reactNativeReanimated.withTiming)(1, configExpanded, () => {
    onAnimatedEndExpanded && (0, _reactNativeReanimated.runOnJS)(onAnimatedEndExpanded)();
  }) : (0, _reactNativeReanimated.withTiming)(0, configCollapsed, () => {
    onAnimatedEndCollapsed && (0, _reactNativeReanimated.runOnJS)(onAnimatedEndCollapsed)();
    if (isUnmountedContent) (0, _reactNativeReanimated.runOnJS)(setUnmountedContent)(true);
  }));
  const style = (0, _reactNativeReanimated.useAnimatedStyle)(() => ({
    height: size.value * progress.value + 1,
    opacity: progress.value === 0 ? 0 : 1
  }));
  const handleCollapsed = (0, _react.useCallback)(() => {
    if (size.value === 0) {
      (0, _reactNativeReanimated.runOnUI)(setUnmountedContent)(false);
      (0, _reactNativeReanimated.runOnUI)(() => {
        'worklet';

        size.value = layout === null || layout === void 0 ? void 0 : layout.height;
      })();
    }

    open.value = !open.value;
    onChangeState && onChangeState(!open.value);
  }, [layout === null || layout === void 0 ? void 0 : layout.height, onChangeState, open, size]);
  const hasLoader = (0, _react.useMemo)(() => isStatusFetching ? handleIndicatorFetching ? handleIndicatorFetching() : /*#__PURE__*/_react.default.createElement(_reactNative.ActivityIndicator, {
    size: "small",
    color: "#AAAAAA",
    style: _styles.styles.indicator
  }) : /*#__PURE__*/_react.default.createElement(_chevron.default, {
    sizeIcon: sizeIcon,
    progress: progress,
    colorIcon: colorIcon,
    handleIcon: handleIcon,
    styleChevron: styleChevron,
    activeBackgroundIcon: activeBackgroundIcon,
    inactiveBackgroundIcon: inactiveBackgroundIcon
  }), [activeBackgroundIcon, colorIcon, handleIcon, handleIndicatorFetching, inactiveBackgroundIcon, isStatusFetching, progress, sizeIcon, styleChevron]);
  const renderHeader = (0, _react.useCallback)(() => {
    return handleCustomTouchable ? handleCustomTouchable() : /*#__PURE__*/_react.default.createElement(_reactNativeReanimated.default.View, {
      style: [_styles.styles.header, styleTouchable]
    }, handleContentTouchable ? handleContentTouchable() : null, isArrow ? hasLoader : null);
  }, [handleCustomTouchable, styleTouchable, handleContentTouchable, isArrow, hasLoader]);
  const content = (0, _react.useCallback)(() => {
    if (isUnmounted && !open.value) {
      return null;
    }

    return renderContent ? renderContent() : null;
  }, [isUnmounted, open.value, renderContent]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.TouchableWithoutFeedback, _extends({
    onPress: handleCollapsed,
    disabled: disabled || isStatusFetching
  }, otherProperty), renderHeader()), /*#__PURE__*/_react.default.createElement(_reactNativeReanimated.default.View, {
    style: [_styles.styles.content, style]
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    onLayout: onLayout,
    style: [_styles.styles.container, styleContainer]
  }, content())));
};

var _default = AnimatedAccordion;
exports.default = _default;
//# sourceMappingURL=accordion.js.map