/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	// The require scope
/******/ 	var __webpack_require__ = {};
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/make namespace object */
/******/ 	(() => {
/******/ 		// define __esModule on exports
/******/ 		__webpack_require__.r = (exports) => {
/******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 			}
/******/ 			Object.defineProperty(exports, '__esModule', { value: true });
/******/ 		};
/******/ 	})();
/******/ 	
/************************************************************************/
var __webpack_exports__ = {};
/*!****************************************!*\
  !*** ./extension/js/public-profile.js ***!
  \****************************************/
__webpack_require__.r(__webpack_exports__);
document.addEventListener('DOMContentLoaded', function () {
  // 1. Grab the UI Elements
  var usernameEl = document.getElementById('public-username');
  var avatarEl = document.getElementById('public-avatar');
  var trustScoreEl = document.getElementById('public-trust-score');
  var locationEl = document.getElementById('public-location');
  var interestsEl = document.getElementById('public-interests');
  var bioEl = document.getElementById('public-bio');

  // NEW: Grab the Annotations UI Elements
  var loadAnnotationsBtn = document.getElementById('load-public-annotations-btn');
  var annotationsListContainer = document.getElementById('public-annotations-list-container');
  var annotationsScrollBox = document.getElementById('public-annotations-scroll-box');
  var copyAnnotationsBtn = document.getElementById('copy-public-annotations-btn');

  // 2. Get the UID of the profile we want to view from the URL
  var urlParams = new URLSearchParams(window.location.search);
  var targetUid = urlParams.get('uid');
  if (!targetUid) {
    usernameEl.textContent = "User not found";
    usernameEl.classList.add('error-message');
    return;
  }

  // Ask the background script to fetch this specific user's public data
  usernameEl.textContent = "Loading Profile...";
  chrome.runtime.sendMessage({
    target: 'offscreen',
    type: 'getPublicProfile',
    uid: targetUid
  }, function (response) {
    if (response && response.success) {
      // Success! Populate the UI with the database info
      var userData = response.data;
      usernameEl.textContent = userData.username || "Anonymous User";
      trustScoreEl.textContent = userData.trustScore || "0";
      locationEl.textContent = userData.location || "Not specified";
      interestsEl.textContent = userData.interests || "Not specified";
      bioEl.textContent = userData.bio || "No bio available.";

      // If they snapped a photo, show it. Otherwise, keep the default.
      if (userData.photoDataUrl) {
        avatarEl.src = userData.photoDataUrl;
      }
    } else {
      // Something went wrong (e.g., bad ID, or user doesn't exist)
      usernameEl.textContent = "Profile not found";
      usernameEl.classList.add('error-message');
      document.querySelectorAll('.info-text').forEach(function (el) {
        return el.textContent = "--";
      });
    }
  });

  // --- NEW: FETCH AND DISPLAY ANNOTATIONS ---
  if (loadAnnotationsBtn) {
    loadAnnotationsBtn.addEventListener('click', function () {
      loadAnnotationsBtn.textContent = "Loading... (Deducting 1 credit)";
      loadAnnotationsBtn.disabled = true;

      // Fetch annotations for this specific target user using the working backend command
      chrome.runtime.sendMessage({
        type: 'fetchUserHistory',
        target: 'background',
        data: {
          uid: targetUid
        }
      }, function (response) {
        loadAnnotationsBtn.textContent = "Show all annotations (Costs 1 credit)";
        loadAnnotationsBtn.disabled = false;
        if (response && response.success) {
          annotationsListContainer.style.display = 'block';
          annotationsScrollBox.innerHTML = ''; // Clear previous items

          if (!response.annotations || response.annotations.length === 0) {
            annotationsScrollBox.innerHTML = '<p style="text-align:center; color:#666;">This user has no annotations yet.</p>';
            return;
          }

          // Loop through the data using our perfected 3-line layout
          response.annotations.forEach(function (ann) {
            var statusColor = ann.isDeleted ? '#dc3545' : '#28a745';
            var statusText = ann.isDeleted ? 'DELETED' : 'ACTIVE';
            var dateStr = "Unknown Date";
            if (ann.createdAt) {
              var dateObj = ann.createdAt.seconds ? new Date(ann.createdAt.seconds * 1000) : new Date(ann.createdAt);
              dateStr = dateObj.toLocaleDateString();
            }

            // STRIP BASE64 IMAGES AND HTML: Force text-only content
            var tempDiv = document.createElement('div');
            tempDiv.innerHTML = ann.anchorText;
            var cleanAnchor = tempDiv.textContent || tempDiv.innerText || "";
            tempDiv.innerHTML = ann.annotationText;
            var cleanNote = tempDiv.textContent || tempDiv.innerText || "";
            var item = document.createElement('div');
            var isRestored = ann.url === "Restored URL";

            // TIGHT SPACING & BLACK LINE
            item.style.borderBottom = '1px solid black';
            item.style.paddingBottom = '8px';
            item.style.marginBottom = '8px';
            item.innerHTML = "\n                            <div style=\"display: flex; justify-content: space-between; align-items: center; margin-bottom: 2px; width: 100%;\">\n                                <div style=\"font-size: 12px; color: #888; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; flex-grow: 1; min-width: 0; padding-right: 10px;\">\n                                    <strong>".concat(dateStr, "</strong> | <span style=\"color: ").concat(statusColor, "; font-weight: bold; margin-right: 5px;\">").concat(statusText, "</span> \n                                    <span style=\"color: #0000AF; font-weight: bold;\">").concat(ann.url, "</span>\n                                </div>\n                                ").concat(isRestored ? '' : "<button class=\"open-url-btn\" style=\"background-color: #0000AF; color: white; border: none; padding: 4px 0; border-radius: 4px; cursor: pointer; font-size: 12px; font-weight: bold; flex-shrink: 0; width: 60px; min-width: 60px; text-align: center;\">Open</button>", "\n                            </div>\n                            \n                            <div style=\"font-size: 14px; color: #333; font-style: italic; background: #eee; padding: 2px 4px; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;\">\"").concat(cleanAnchor, "\"</div>\n                            \n                            <div style=\"font-size: 14px; color: #555; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 2px;\">").concat(cleanNote, "</div>\n                        ");
            if (!isRestored) {
              var btn = item.querySelector('.open-url-btn');
              btn.addEventListener('click', function () {
                window.open(ann.url, '_blank');
              });
            }
            annotationsScrollBox.appendChild(item);
          });
        } else {
          var errorMsg = response && response.error ? response.error : "Could not load annotations. You may not have enough credits.";
          alert("Error: " + errorMsg);
        }
      });
    });
  }

  // --- NEW: COPY LIST BUTTON LOGIC ---
  if (copyAnnotationsBtn) {
    copyAnnotationsBtn.addEventListener('click', function () {
      // Grabs all the visible text from the scroll box, ignoring the HTML structure
      var textToCopy = annotationsScrollBox.innerText;
      navigator.clipboard.writeText(textToCopy).then(function () {
        var originalText = copyAnnotationsBtn.textContent;
        copyAnnotationsBtn.textContent = "Copied!";
        setTimeout(function () {
          copyAnnotationsBtn.textContent = originalText;
        }, 2000);
      });
    });
  }

  // Close button logic
  document.getElementById('close-btn').addEventListener('click', function () {
    window.close();
  });
});
/******/ })()
;
//# sourceMappingURL=public-profile.js.map