"use strict";

var _wp = wp,
  apiFetch = _wp.apiFetch;
jQuery(function ($) {
  var gutenify_fashionstoreRedirectToKitPage = function gutenify_fashionstoreRedirectToKitPage(res) {
    // if( res?.status && 'active' === res.status ) {
    window.location = "".concat(window.gutenify_fashionstore.gutenify_kit_gallery);
    // }
  };

  // Activate Gutenify.
  $(document).on('click', '.gutenify-fashionstore-activate-gutenify', function () {
    $(this).html('<span class="dashicons dashicons-update"></span> Loading...').addClass('gutenify-fashionstore-importing-gutenify');
    apiFetch({
      path: '/wp/v2/plugins/gutenify/gutenify',
      method: 'POST',
      data: {
        "status": "active"
      }
    }).then(function (res) {
      gutenify_fashionstoreRedirectToKitPage(res);
    })["catch"](function () {
      gutenify_fashionstoreRedirectToKitPage({});
    });
  });
  $(document).on('click', '.gutenify-fashionstore-install-gutenify', function () {
    $(this).html('<span class="dashicons dashicons-update"></span> Loading...').addClass('gutenify-fashionstore-importing-gutenify');
    apiFetch({
      path: '/wp/v2/plugins',
      method: 'POST',
      data: {
        "slug": "gutenify",
        "status": "active"
      }
    }).then(function (res) {
      gutenify_fashionstoreRedirectToKitPage(res);
    })["catch"](function () {
      gutenify_fashionstoreRedirectToKitPage({});
    });
  });
  $(document).on('click', '.gutenify-fashionstore-admin-notice .notice-dismiss', function () {
    console.log(ajaxurl + "?action=gutenify_fashionstore_hide_theme_info_noticebar");
    apiFetch({
      url: ajaxurl + "?action=gutenify_fashionstore_hide_theme_info_noticebar&gutenify_fashionstore-nonce-name=".concat(window.gutenify_fashionstore.gutenify_fashionstore_nonce),
      method: 'POST'
    }).then(function (res) {
      console.log(res);
    })["catch"](function (res) {
      console.log(res);
    });
  });

  // Open video modal 1 (Themes)
  $(document).on('click', '.btn-play-overlay-kits', function () {
    var modal = $('#gutenify-theme-modal-1');
    var iframe = modal.find('iframe');
    iframe.attr('src', iframe.attr('data-src'));
    modal.fadeIn(200);
  });

  // Open video modal 2 (Patterns)
  $(document).on('click', '.btn-play-overlay-patterns', function () {
    var modal = $('#gutenify-theme-modal-2');
    var video = modal.find('video');
    var source = video.find('source');
    source.attr('src', source.attr('data-src'));
    video[0].load();
    modal.fadeIn(200);
  });

  // Close modals
  $(document).on('click', '.gutenify-video-modal-close, .gutenify-video-modal-overlay', function (e) {
    if ($(e.target).hasClass('gutenify-video-modal-overlay') || $(e.target).hasClass('gutenify-video-modal-close')) {
      var modal = $('.gutenify-video-modal-overlay');
      modal.fadeOut(200, function () {
        // stop videos/iframes
        var iframe = modal.find('iframe');
        iframe.attr('src', '');
        var video = modal.find('video');
        if (video.length > 0) {
          video[0].pause();
          video.find('source').attr('src', '');
        }
      });
    }
  });

  // Tab switching logic
  $(document).on('click', '.gutenify-admin-tab-btn', function () {
    var tabId = $(this).data('tab');
    $('.gutenify-admin-tab-btn').removeClass('active');
    $(this).addClass('active');
    $('.gutenify-tab-content').hide();
    $('#' + tabId).fadeIn(200);
  });
});