// Define the input script
var script = "INT. LIVING ROOM - DAY\n\nMARY is sitting on the couch reading a book. The doorbell rings.\n\nMARY\n(to herself)\nWho could that be?";

// Define the API endpoints
var unsplashApiUrl = "https://api.unsplash.com/photos/random?query=";
var giphyApiUrl = "https://api.giphy.com/v1/gifs/search?api_key=YOUR_API_KEY&q=";

// Define the search terms
var searchTerms = ["living room", "couch", "book", "doorbell", "surprise"];

// Generate a storyboard based on the input script
var storyboard = [];
script.split("\n").forEach(function(line) {
  if (line.trim() !== "") {
    var searchTerm = searchTerms[Math.floor(Math.random() * searchTerms.length)];
    var imageUrl = "";
    var gifUrl = "";
    fetch(unsplashApiUrl + searchTerm)
      .then(function(response) {
        return response.json();
      })
      .then(function(data) {
        imageUrl = data.urls.regular;
        return fetch(giphyApiUrl + searchTerm);
      })
      .then(function(response) {
        return response.json();
      })
      .then(function(data) {
        gifUrl = data.data[0].url;
        var scene = {
          line: line.trim(),
          image_url: imageUrl,
          gif_url: gifUrl
        };
        storyboard.push(scene);
      })
      .catch(function(error) {
        console.log(error);
      });
  }
});

// Display the storyboard
storyboard.forEach(function(scene) {
  console.log(scene.line);
  console.log("Image:", scene.image_url);
  console.log("GIF:", scene.gif_url);
  console.log("--------------------");
});