"use strict";

import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, TouchableOpacity, StyleSheet, Modal, Platform, ScrollView, Image } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { useThemeStyles } from '../../hooks/useThemeStyles';
import { useColorScheme } from '../../hooks/use-color-scheme';
import { useI18n } from '../../hooks/useI18n';
import { fontFamilies } from '../../styles/fonts';
import { useProfileEditing } from '../../hooks/useProfileEditing';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export const EditLinksModal = ({
  visible,
  onClose,
  initialLinks = [],
  theme = 'light',
  onSave
}) => {
  const {
    t
  } = useI18n();
  const colorScheme = useColorScheme();
  const themeStyles = useThemeStyles(theme || 'light', colorScheme);
  const colors = themeStyles.colors;
  const {
    saveProfile,
    isSaving
  } = useProfileEditing();
  const [links, setLinks] = useState(initialLinks);
  const [newLinkUrl, setNewLinkUrl] = useState('');
  useEffect(() => {
    if (visible) {
      setLinks(initialLinks);
      setNewLinkUrl('');
    }
  }, [visible, initialLinks]);
  const handleAddLink = () => {
    if (!newLinkUrl.trim()) return;
    const link = {
      id: `link-${Date.now()}`,
      url: newLinkUrl.trim(),
      title: newLinkUrl.replace(/^https?:\/\//, '').replace(/\/$/, ''),
      description: `Link to ${newLinkUrl}`
    };
    setLinks([...links, link]);
    setNewLinkUrl('');
  };
  const handleRemoveLink = id => {
    setLinks(links.filter(link => link.id !== id));
  };
  const handleSave = async () => {
    const success = await saveProfile({
      linksMetadata: links,
      links: links.map(link => link.url)
    });
    if (success) {
      onSave?.();
      onClose();
    }
  };
  return /*#__PURE__*/_jsx(Modal, {
    visible: visible,
    animationType: "slide",
    transparent: true,
    onRequestClose: onClose,
    children: /*#__PURE__*/_jsx(View, {
      style: styles.modalOverlay,
      children: /*#__PURE__*/_jsxs(View, {
        style: [styles.modalContent, {
          backgroundColor: colors.background
        }],
        children: [/*#__PURE__*/_jsxs(View, {
          style: styles.modalHeader,
          children: [/*#__PURE__*/_jsx(TouchableOpacity, {
            onPress: onClose,
            style: styles.closeButton,
            children: /*#__PURE__*/_jsx(Ionicons, {
              name: "close",
              size: 24,
              color: colors.text
            })
          }), /*#__PURE__*/_jsx(Text, {
            style: [styles.modalTitle, {
              color: colors.text
            }],
            children: t('editProfile.items.links.title') || 'Links'
          }), /*#__PURE__*/_jsx(TouchableOpacity, {
            onPress: handleSave,
            disabled: isSaving,
            style: [styles.saveButton, {
              opacity: isSaving ? 0.5 : 1
            }],
            children: /*#__PURE__*/_jsx(Text, {
              style: [styles.saveButtonText, {
                color: colors.tint
              }],
              children: isSaving ? 'Saving...' : 'Save'
            })
          })]
        }), /*#__PURE__*/_jsxs(ScrollView, {
          style: styles.modalBody,
          children: [/*#__PURE__*/_jsxs(View, {
            style: styles.inputGroup,
            children: [/*#__PURE__*/_jsx(Text, {
              style: [styles.label, {
                color: colors.text
              }],
              children: t('editProfile.items.links.add') || 'Add Link'
            }), /*#__PURE__*/_jsxs(View, {
              style: styles.addLinkRow,
              children: [/*#__PURE__*/_jsx(TextInput, {
                style: [styles.input, {
                  backgroundColor: colors.card,
                  color: colors.text,
                  borderColor: colors.border,
                  flex: 1
                }],
                value: newLinkUrl,
                onChangeText: setNewLinkUrl,
                placeholder: t('editProfile.items.links.placeholder') || 'Enter URL (e.g., https://example.com)',
                placeholderTextColor: colors.secondaryText,
                keyboardType: "url",
                autoCapitalize: "none",
                autoCorrect: false,
                selectionColor: colors.tint
              }), /*#__PURE__*/_jsx(TouchableOpacity, {
                style: [styles.addButton, {
                  backgroundColor: colors.tint
                }],
                onPress: handleAddLink,
                disabled: !newLinkUrl.trim(),
                children: /*#__PURE__*/_jsx(Ionicons, {
                  name: "add",
                  size: 20,
                  color: "#fff"
                })
              })]
            })]
          }), links.length > 0 && /*#__PURE__*/_jsxs(View, {
            style: styles.linksList,
            children: [/*#__PURE__*/_jsxs(Text, {
              style: [styles.listTitle, {
                color: colors.text
              }],
              children: [t('editProfile.items.links.yourLinks') || 'Your Links', " (", links.length, ")"]
            }), links.map((link, index) => /*#__PURE__*/_jsxs(View, {
              style: [styles.linkItem, {
                backgroundColor: colors.card,
                borderColor: colors.border
              }, index < links.length - 1 && {
                borderBottomWidth: StyleSheet.hairlineWidth
              }],
              children: [link.image && /*#__PURE__*/_jsx(Image, {
                source: {
                  uri: link.image
                },
                style: styles.linkImage
              }), /*#__PURE__*/_jsxs(View, {
                style: styles.linkInfo,
                children: [/*#__PURE__*/_jsx(Text, {
                  style: [styles.linkTitle, {
                    color: colors.text
                  }],
                  numberOfLines: 1,
                  children: link.title || link.url
                }), link.description && link.description !== link.title && /*#__PURE__*/_jsx(Text, {
                  style: [styles.linkDescription, {
                    color: colors.secondaryText
                  }],
                  numberOfLines: 1,
                  children: link.description
                }), /*#__PURE__*/_jsx(Text, {
                  style: [styles.linkUrl, {
                    color: colors.secondaryText
                  }],
                  numberOfLines: 1,
                  children: link.url
                })]
              }), /*#__PURE__*/_jsx(TouchableOpacity, {
                onPress: () => handleRemoveLink(link.id),
                style: styles.removeButton,
                children: /*#__PURE__*/_jsx(Ionicons, {
                  name: "trash-outline",
                  size: 18,
                  color: "#FF3B30"
                })
              })]
            }, link.id))]
          })]
        })]
      })
    })
  });
};
const styles = StyleSheet.create({
  modalOverlay: {
    flex: 1,
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
    justifyContent: 'flex-end'
  },
  modalContent: {
    borderTopLeftRadius: 20,
    borderTopRightRadius: 20,
    paddingTop: Platform.OS === 'ios' ? 20 : 16,
    maxHeight: '80%'
  },
  modalHeader: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingHorizontal: 16,
    paddingBottom: 16,
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderBottomColor: '#E5E5EA'
  },
  closeButton: {
    width: 40,
    height: 40,
    alignItems: 'center',
    justifyContent: 'center'
  },
  modalTitle: {
    fontSize: 18,
    fontWeight: '600',
    fontFamily: fontFamilies.phuduSemiBold,
    flex: 1,
    textAlign: 'center'
  },
  saveButton: {
    paddingHorizontal: 16,
    paddingVertical: 8
  },
  saveButtonText: {
    fontSize: 16,
    fontWeight: '600',
    fontFamily: fontFamilies.phuduSemiBold
  },
  modalBody: {
    padding: 16
  },
  inputGroup: {
    gap: 8,
    marginBottom: 24
  },
  label: {
    fontSize: 14,
    fontWeight: '600',
    fontFamily: fontFamilies.phuduSemiBold
  },
  addLinkRow: {
    flexDirection: 'row',
    gap: 8,
    alignItems: 'center'
  },
  input: {
    borderWidth: StyleSheet.hairlineWidth,
    borderRadius: 12,
    padding: 16,
    fontSize: 16,
    minHeight: 52
  },
  addButton: {
    width: 52,
    height: 52,
    borderRadius: 12,
    alignItems: 'center',
    justifyContent: 'center'
  },
  linksList: {
    gap: 8
  },
  listTitle: {
    fontSize: 16,
    fontWeight: '600',
    fontFamily: fontFamilies.phuduSemiBold,
    marginBottom: 8
  },
  linkItem: {
    flexDirection: 'row',
    alignItems: 'center',
    padding: 16,
    borderRadius: 12,
    borderWidth: StyleSheet.hairlineWidth,
    gap: 12
  },
  linkImage: {
    width: 40,
    height: 40,
    borderRadius: 8
  },
  linkInfo: {
    flex: 1,
    gap: 4
  },
  linkTitle: {
    fontSize: 16,
    fontWeight: '600'
  },
  linkDescription: {
    fontSize: 14
  },
  linkUrl: {
    fontSize: 12
  },
  removeButton: {
    padding: 8
  }
});
//# sourceMappingURL=EditLinksModal.js.map