function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
import * as React from "react";
import { Animated, View, StyleSheet } from "react-native";
import color from "color";
import ActivityIndicator from "./ActivityIndicator";
// import Icon, { IconSource } from './Icon';
import Surface from "./Surface";
import Text from "./Typography/Text";
import TouchableRipple from "./TouchableRipple/TouchableRipple.native";
import { colors as Colors } from "../styles/tokens";
import { withTheme } from "../core/theming";
const Button = _ref => {
  var _StyleSheet$flatten;
  let {
    disabled,
    mode = "contained",
    loading,
    color: buttonColor,
    children,
    accessibilityLabel,
    accessibilityHint,
    onPress,
    onLongPress,
    style,
    theme,
    contentStyle,
    labelStyle,
    testID,
    accessible,
    mt = 0,
    ...rest
  } = _ref;
  const {
    current: elevation
  } = React.useRef(new Animated.Value(disabled || mode !== "contained" ? 0 : 2));
  React.useEffect(() => {
    elevation.setValue(disabled || mode !== "contained" ? 0 : 2);
  }, [mode, elevation, disabled]);
  const baseButtonColor = buttonColor ? buttonColor : theme.colors.primary.default;
  const handlePressIn = () => {
    if (mode === "contained") {
      const {
        scale
      } = theme.animation;
      // Animated.timing(elevation, {
      //   toValue: 8,
      //   duration: 200 * scale,
      //   useNativeDriver: true,
      // }).start();
    }
  };

  const handlePressOut = () => {
    if (mode === "contained") {
      const {
        scale
      } = theme.animation;
      // Animated.timing(elevation, {
      //   toValue: 2,
      //   duration: 150 * scale,
      //   useNativeDriver: true,
      // }).start();
    }
  };

  const {
    colors,
    roundness
  } = theme;
  const font = theme.fonts.bold;
  let backgroundColor, borderColor, textColor, borderWidth;
  if (mode === "contained") {
    if (disabled) {
      backgroundColor = color(theme.dark ? Colors.white : baseButtonColor).alpha(0.12).rgb().string();
    } else if (buttonColor) {
      backgroundColor = buttonColor;
    } else {
      backgroundColor = theme.colors.primary.default;
    }
  } else {
    backgroundColor = "transparent";
  }
  if (mode === "outlined") {
    if (disabled) {
      borderColor = color(baseButtonColor).alpha(0.12).rgb().string();
    } else {
      borderColor = baseButtonColor;
    }
    borderWidth = 3;
  } else {
    borderColor = "transparent";
    borderWidth = 0;
  }
  if (disabled) {
    textColor = color(baseButtonColor).alpha(0.32).rgb().string();
  } else if (mode === "contained") {
    textColor = Colors.white;
  } else {
    textColor = baseButtonColor;
  }
  const rippleColor = color(textColor).alpha(0.32).rgb().string();
  const buttonStyle = {
    backgroundColor,
    borderColor,
    borderWidth,
    borderRadius: roundness
  };
  const touchableStyle = {
    borderRadius: style ? (StyleSheet.flatten(style) || {}).borderRadius || roundness : roundness
  };
  const {
    color: customLabelColor,
    fontSize: customLabelSize
  } = StyleSheet.flatten(labelStyle) || {};
  const textStyle = {
    color: textColor,
    ...font
  };
  const iconStyle = ((_StyleSheet$flatten = StyleSheet.flatten(contentStyle)) === null || _StyleSheet$flatten === void 0 ? void 0 : _StyleSheet$flatten.flexDirection) === "row-reverse" ? styles.iconReverse : styles.icon;
  return /*#__PURE__*/React.createElement(Surface, _extends({}, rest, {
    style: [styles.button, {
      elevation
    }, buttonStyle, {
      marginTop: mt
    }, style]
  }), /*#__PURE__*/React.createElement(TouchableRipple, {
    borderless: true,
    delayPressIn: 0,
    onPress: onPress,
    onLongPress: onLongPress,
    onPressIn: handlePressIn,
    onPressOut: handlePressOut,
    accessibilityLabel: accessibilityLabel,
    accessibilityHint: accessibilityHint,
    accessibilityTraits: disabled ? ["button", "disabled"] : "button",
    accessibilityComponentType: "button",
    accessibilityRole: "button",
    accessibilityState: {
      disabled
    },
    accessible: accessible,
    disabled: disabled,
    rippleColor: rippleColor,
    style: touchableStyle,
    testID: testID
  }, /*#__PURE__*/React.createElement(View, {
    style: [styles.content, contentStyle]
  }, loading ? /*#__PURE__*/React.createElement(ActivityIndicator, {
    size: customLabelSize ?? 16,
    color: typeof customLabelColor === "string" ? customLabelColor : textColor,
    style: iconStyle
  }) : null, /*#__PURE__*/React.createElement(Text, {
    selectable: false,
    numberOfLines: 1,
    style: [styles.label, textStyle, font, labelStyle]
  }, children))));
};
const styles = StyleSheet.create({
  button: {
    // minWidth: 64,
    borderStyle: "solid",
    width: "100%"
  },
  content: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center"
  },
  icon: {
    marginLeft: 18,
    marginRight: -16
  },
  iconReverse: {
    marginRight: 12,
    marginLeft: -4
  },
  label: {
    textAlign: "center",
    letterSpacing: 1,
    marginVertical: 16,
    marginHorizontal: 24
  }
});
export default withTheme(Button);
//# sourceMappingURL=Button.js.map