"use strict";

import React, { useContext } from 'react';
import { StyleSheet, View } from 'react-native';
import { ApplicationContext } from "../Context/index.js";
import { Button } from "../Button/index.js";
import { Image } from "../Image/index.js";
import { Radius, Spacing, Styles } from "../Consts/index.js";
import { Text } from "../Text/index.js";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const Popup = ({
  image,
  title = 'Title',
  description,
  information,
  secondary,
  primary = {
    title: 'Primary',
    onPress: () => {}
  },
  buttonDirection = 'row',
  onRequestClose
}) => {
  const {
    theme,
    navigator
  } = useContext(ApplicationContext);

  /**
   * on action popup
   * @param callback
   */
  const onAction = callback => {
    if (typeof onRequestClose === 'function') {
      onRequestClose?.(callback);
    } else {
      navigator?.pop();
      setTimeout(() => {
        callback?.();
      }, 300);
    }
  };

  /**
   *  build main action of popup
   */
  const buildAction = () => {
    const renderRow = () => {
      return /*#__PURE__*/_jsxs(View, {
        style: Styles.rowCenter,
        children: [!!secondary?.title && /*#__PURE__*/_jsxs(_Fragment, {
          children: [/*#__PURE__*/_jsx(View, {
            style: Styles.flex,
            children: /*#__PURE__*/_jsx(Button, {
              title: secondary?.title,
              type: "text",
              size: "medium",
              onPress: () => {
                onAction(secondary?.onPress);
              }
            })
          }), /*#__PURE__*/_jsx(View, {
            style: styles.buttonSpace
          })]
        }), /*#__PURE__*/_jsx(View, {
          style: Styles.flex,
          children: /*#__PURE__*/_jsx(Button, {
            title: primary.title,
            size: "medium",
            onPress: () => {
              onAction(primary.onPress);
            }
          })
        })]
      });
    };
    const renderColumn = () => {
      return /*#__PURE__*/_jsxs(View, {
        style: Styles.columnCenter,
        children: [/*#__PURE__*/_jsx(Button, {
          title: primary.title,
          size: "medium",
          onPress: () => {
            onAction(primary.onPress);
          }
        }), !!secondary?.title && /*#__PURE__*/_jsxs(_Fragment, {
          children: [/*#__PURE__*/_jsx(View, {
            style: styles.buttonSpace
          }), /*#__PURE__*/_jsx(Button, {
            title: secondary?.title,
            type: "text",
            size: "medium",
            onPress: () => {
              onAction(secondary?.onPress);
            }
          })]
        })]
      });
    };
    if (buttonDirection === 'auto') {
      if (secondary?.title && (secondary.title.length > 12 || primary.title.length > 12)) {
        return renderColumn();
      }
      return renderRow();
    }
    if (buttonDirection === 'row') {
      return renderRow();
    }
    return renderColumn();
  };

  /**
   * build description
   */
  const buildDescription = () => {
    if (typeof description === 'string') {
      return /*#__PURE__*/_jsx(Text, {
        typography: 'subhead',
        numberOfLines: 3,
        children: description
      });
    }
    return description;
  };
  return /*#__PURE__*/_jsxs(View, {
    style: [styles.container, {
      backgroundColor: theme.colors.background.surface
    }],
    children: [!!image && /*#__PURE__*/_jsx(Image, {
      source: {
        uri: image
      },
      style: styles.image
    }), /*#__PURE__*/_jsxs(View, {
      style: styles.content,
      children: [/*#__PURE__*/_jsx(Text, {
        typography: 'callout',
        fontWeight: 'bold',
        numberOfLines: 1,
        children: title
      }), /*#__PURE__*/_jsx(View, {
        style: styles.description,
        children: buildDescription()
      }), !!information && /*#__PURE__*/_jsx(View, {
        style: styles.information,
        children: /*#__PURE__*/_jsx(Text, {
          typography: 'caption2',
          color: theme.colors.text.hint,
          numberOfLines: 1,
          children: information
        })
      })]
    }), /*#__PURE__*/_jsx(View, {
      style: styles.contentAction,
      children: buildAction()
    })]
  });
};
const styles = StyleSheet.create({
  container: {
    borderRadius: Radius.M,
    minWidth: '100%'
  },
  image: {
    width: '100%',
    borderTopLeftRadius: Radius.M,
    borderTopRightRadius: Radius.M,
    aspectRatio: 16 / 9
  },
  content: {
    padding: Spacing.XL
  },
  description: {
    marginTop: Spacing.S
  },
  information: {
    marginTop: Spacing.S
  },
  contentAction: {
    paddingHorizontal: Spacing.XL,
    paddingBottom: Spacing.XL
  },
  buttonSpace: {
    width: Spacing.S,
    height: Spacing.S
  }
});
export default Popup;
//# sourceMappingURL=Popup.js.map