import confirmationModalTemplate from '../shared/confirmationModalTemplate'
import quantityStepperTemplate from '../shared/quantityStepperTemplate'

const templateGuest = `
<div id="saveto-wishlis-main-app">
  <div v-if="!wishlist.isLoggedIn" class="saveto-wishlist-lists-wrapper">
    <h2 class="stwlite-tw-wishlist-title" :style="{ fontSize: wishlist.pageStyles?.headingTitleSize + 'px', color: wishlist.pageStyles?.headingTitleColor }">{{ wishlist.guestWishlist }}</h2>
    <p class="stwlite-tw-subtitle" :style="{ fontSize: wishlist.pageStyles?.headingTextSize + 'px', color: wishlist.pageStyles?.headingTextColor }">
      <a :href="wishlist.pageUrls.myAccountUrl" class="wishlist-empty-action-btn">
        {{ wishlist.guestWishlistDescription }}
      </a>
    </p>
    <div class="saveto-wishlist-container stwlite-tw-item-wishlist">
      <div class="stwlite-tw-wishlist-item-links">
        <a :href="wishlist.pageUrls.shopUrl" class="stwlite-tw-wishlist-item-link">
          {{ wishlist.continueShopping }}
        </a>
      </div>

      <div class="stwlite-tw-search-wrapper" :class="{ 'stwlite-tw-has-content': search }">
        <svg class="stwlite-tw-search-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor">
          <circle cx="11" cy="11" r="8"></circle>
          <path d="M21 21l-4.35-4.35"></path>
        </svg>
        <input
          v-model="search"
          type="text"
          class="stwlite-tw-search-input"
          :placeholder="wishlist.searchPlaceholder"
          :aria-label="wishlist.searchLabel"
          @input="onSearch"
        >
        <button type="button" class="stwlite-tw-clear-button" :aria-label="wishlist.clearSearch" @click="clearSearch">
          &times;
        </button>
      </div>
    </div>
    
    <div v-if="!fetchingItems" class="stwlite-tw-table-wrapper">
      <div class="stwlite-tw-table-container">
        <table class="stwlite-tw-data-table" :style="{ '--table-column-text-size': wishlist.pageStyles?.tableColumnTextSize + 'px', '--table-row-text-size': wishlist.pageStyles?.tableRowTextSize + 'px' }">
          <thead>
            <tr>
              <th class="stwlite-tw-checkbox-col">
                <input
                  type="checkbox"
                  :checked="allSelected"
                  @change="toggleSelectAll"
                  class="stwlite-tw-wishlist-checkbox"
                />
              </th>
              <th v-if="wishlist.pageStyles?.showProductImage" data-column="image" :style="{ fontSize: wishlist.pageStyles?.tableColumnTextSize + 'px' }">
                {{ wishlist.table.product_table.image }}
              </th>
              <th data-column="name" :style="{ fontSize: wishlist.pageStyles?.tableColumnTextSize + 'px' }">
                {{ wishlist.table.product_table.name }}
              </th>
              <th data-column="price" :style="{ fontSize: wishlist.pageStyles?.tableColumnTextSize + 'px' }">
                {{ wishlist.table.product_table.price }}
              </th>
              <th data-column="stock_status" :style="{ fontSize: wishlist.pageStyles?.tableColumnTextSize + 'px' }">
                {{ wishlist.table.product_table.stock_status }}
              </th>
              <th data-column="quantity" :style="{ fontSize: wishlist.pageStyles?.tableColumnTextSize + 'px' }">
                {{ wishlist.table.product_table.quantity }}
              </th>
              <th data-column="subtotal" :style="{ fontSize: wishlist.pageStyles?.tableColumnTextSize + 'px' }">
                {{ wishlist.table.product_table.subtotal }}
              </th>
              <th data-column="actions" :style="{ fontSize: wishlist.pageStyles?.tableColumnTextSize + 'px' }">
                {{ wishlist.table.product_table.actions }}
              </th>
            </tr>
          </thead>
          <tbody v-if="lists.length > 0" id="tableBody">
            <tr v-for="(item, index) in lists" :key="item.id" :style="{ fontSize: wishlist.pageStyles?.tableRowTextSize + 'px' }">
              <td class="stwlite-tw-checkbox-col">
                <input
                  type="checkbox"
                  :checked="selectedItems.includes(item.product.id)"
                  @change="toggleSelectItem(item.product.id)"
                  class="stwlite-tw-wishlist-checkbox"
                />
              </td>
              <td v-if="wishlist.pageStyles?.showProductImage" class="stwlite-tw-product-image-cell">
                <a :href="item.product.link">
                  <img
                    :src="item.product.image_url"
                    :alt="item.product.name"
                    class="stwlite-tw-product-thumbnail"
                    loading="lazy"
                  />
                </a>
              </td>
              <td>
                <div class="stwlite-tw-wishlist-item-name">
                  <a :href="item.product.link">{{ item.product.name }}</a>
                </div>
                <div v-if="item.product.type === 'variable' && item.product.variations?.attributes && Object.keys(item.product.variations?.attributes).length > 0" class="stwlite-tw-wishlist-item-actions">
                  <div v-for="(attributes, attribute) in item.product.variations.attributes" :key="attribute+item.product.id">
                    <label :for="attribute">{{ item.product.variations.attribute_labels?.[attribute] || attribute.replace('pa_', '').charAt(0).toUpperCase() + attribute.replace('pa_', '').slice(1) }}</label>
                    <select
                      v-model="selectedDefaultVariation[item.product.id][attribute]"
                      :key="attribute"
                      @change="handleVariationChange(item.product)"
                      :id="attribute+item.product.id"
                      class="stwlite-tw-wishlist-item-variation-select"
                    >
                      <option v-for="(attr, indx) in attributes" :key="indx" :value="attr.name">{{ attr.name }}</option>
                    </select>
                  </div>
                </div>
              </td>
              <td>
                <span v-if="hasNoPrice(item)">&mdash;</span>
                <span v-else v-html="item.product.price_formatted"></span>
              </td>
              <td>
                <template v-if="item.product.stock === 'instock'">
                  <svg style="vertical-align: middle; color: green;" width="16" height="16" viewBox="0 0 16 16" fill="none">
                    <title>In stock</title>
                    <circle cx="8" cy="8" r="8" fill="currentColor" />
                    <path d="M4.5 8.5L7 11L11.5 6.5" stroke="#fff" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
                  </svg>
                  <span style="color: green; margin-left: 4px;">{{ wishlist.table.in_stock }}</span>
                </template>
                <template v-else-if="item.product.stock === 'onbackorder'">
                  <svg style="vertical-align: middle; color: #FFD600;" width="16" height="16" viewBox="0 0 16 16" fill="none">
                    <title>Allow backorder</title>
                    <circle cx="8" cy="8" r="8" fill="currentColor" />
                    <path d="M4.5 8.5L7 11L11.5 6.5" stroke="#fff" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
                  </svg>
                  <span style="color: #FFD600; margin-left: 4px;">{{ wishlist.table.allow_backorder }}</span>
                </template>
                <template v-else>
                  <svg style="vertical-align: middle; color: red;" width="16" height="16" viewBox="0 0 16 16" fill="none">
                    <title>Out of stock</title>
                    <circle cx="8" cy="8" r="8" fill="currentColor" />
                    <line x1="5" y1="5" x2="11" y2="11" stroke="#fff" stroke-width="1.5" stroke-linecap="round"/>
                    <line x1="11" y1="5" x2="5" y2="11" stroke="#fff" stroke-width="1.5" stroke-linecap="round"/>
                  </svg>
                  <span style="color: red; margin-left: 4px;">{{ wishlist.table.out_of_stock }}</span>
                </template>
              </td>
              <td>
                ${quantityStepperTemplate}
              </td>
              <td><span v-html="calculateSubtotal(item.product, item.quantity)"></span></td>
              <td>
                <div class="stwlite-tw-wishlist-items-actions">
                  <a
                    v-if="item.product.type === 'variable' && selectedVariation[item.product.id] === ''"
                    :href="item.product.link"
                    class="stwlite-tw-table-btn stwlite-tw-table-btn-add-to-cart-button"
                    :style="{ fontSize: wishlist.pageStyles?.buttonTextSize + 'px', color: wishlist.pageStyles?.buttonTextColor, backgroundColor: wishlist.pageStyles?.buttonBackgroundColor }"
                  >
                    {{ wishlist.table.select_options }}
                  </a>
                  <button
                    v-else
                    type="button"
                    class="stwlite-tw-table-btn stwlite-tw-table-btn-add-to-cart-button"
                    :style="{ fontSize: wishlist.pageStyles?.buttonTextSize + 'px', color: wishlist.pageStyles?.buttonTextColor, backgroundColor: wishlist.pageStyles?.buttonBackgroundColor }"
                    @click="handleAddToCart(item.product.type, item.collection_id, item.product.id, item.quantity)"
                    :disabled="item.product.stock === 'outofstock' || (addingItemId === item.product.id && addingItem) || (item.product.type === 'variable' && selectedVariation[item.product.id] === '') || hasNoPrice(item)"
                  >
                    {{ addingItemId === item.product.id && addingItem ? wishlist.table.adding_item : wishlist.table.add_to_cart }}
                  </button>
                  <button
                    type="button"
                    class="stwlite-tw-table-btn stwlite-tw-table-btn-delete-product-button"
                    @click="handleDeleteItem(item.product.id)"
                    :disabled="deletingItem && deletingItemId === item.product.id"
                  >
                    {{ wishlist.table.delete }}
                  </button>
                </div>
              </td>
            </tr>
          </tbody>
          <tfoot v-else>
            <tr>
              <td colspan="8">
                <div class="saveto-wishlist-empty-container">
                  <h2>{{ wishlist.empty.title }}</h2>
                </div>
              </td>
            </tr>
          </tfoot>
        </table>
      </div>

      <div class="stwlite-tw-table-footer">
        <div class="stwlite-tw-table-info">
          <span id="tableInfo">{{ tableInfo }}</span>
        </div>
    
        <div class="stwlite-tw-pagination stwlite-tw-flex stwlite-tw-flex-col sm:stwlite-tw-flex-row stwlite-tw-items-center stwlite-tw-justify-between sm:stwlite-tw-gap-4 stwlite-tw-gap-3">
          <div class="stwlite-tw-items-per-page stwlite-tw-w-full sm:stwlite-tw-w-auto stwlite-tw-flex stwlite-tw-justify-center sm:stwlite-tw-justify-start">
            <label for="itemsSelect" class="stwlite-tw-whitespace-nowrap">{{ wishlist.table.show }}:</label>
            <select v-model="pagination.pageSize" id="itemsSelect" @change="handleItemsPerPageChange" class="stwlite-tw-ml-2">
              <option value="5">5</option>
              <option value="10">10</option>
              <option value="15">15</option>
            </select>
          </div>

          <div class="stwlite-tw-flex stwlite-tw-items-center stwlite-tw-justify-center stwlite-tw-gap-2 stwlite-tw-flex-wrap">
            <button class="stwlite-tw-pagination-btn" id="prevBtn" :style="{ fontSize: wishlist.pageStyles?.buttonTextSize + 'px', color: wishlist.pageStyles?.buttonTextColor, backgroundColor: wishlist.pageStyles?.buttonBackgroundColor }" @click="handlePagination('prev')" :disabled="pagination.current === 1">
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <path d="M15 18l-6-6 6-6"></path>
              </svg>
              <span class="stwlite-tw-pagination-btn-text">{{ wishlist.table.previous }}</span>
            </button>
    
            <div class="stwlite-tw-pagination-numbers" id="paginationNumbers" v-if="pagination.totalPages > 0">
              <button
                :class="['stwlite-tw-pagination-number', { 'active': i === pagination.current }]"
                :style="{ fontSize: wishlist.pageStyles?.buttonTextSize + 'px', color: wishlist.pageStyles?.buttonTextColor, backgroundColor: wishlist.pageStyles?.buttonBackgroundColor, opacity: i === pagination.current ? 1 : 0.5 }"
                v-for="i in pagination.totalPages"
                :key="i"
                @click="handlePaginationNumber(i)"
                :disabled="i === pagination.current"
              >{{ i }}</button>
            </div>
    
            <button class="stwlite-tw-pagination-btn" id="nextBtn" :style="{ fontSize: wishlist.pageStyles?.buttonTextSize + 'px', color: wishlist.pageStyles?.buttonTextColor, backgroundColor: wishlist.pageStyles?.buttonBackgroundColor }" @click="handlePagination('next')" :disabled="pagination.current === pagination.totalPages">
              <span class="stwlite-tw-pagination-btn-text">{{ wishlist.table.next }}</span>
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <path d="M9 18l6-6-6-6"></path>
              </svg>
            </button>
          </div>
        </div>
      </div>

      <div class="stwlite-tw-table-footer-actions">
        <div class="stwlite-tw-table-footer-actions-left">
          <button
            v-if="lists.length > 0"
            type="button"
            class="stwlite-tw-table-btn stwlite-tw-table-btn-add-to-cart-button stwlite-tw-add-all-to-cart-btn"
            :style="{ fontSize: wishlist.pageStyles?.buttonTextSize + 'px', color: wishlist.pageStyles?.buttonTextColor, backgroundColor: wishlist.pageStyles?.buttonBackgroundColor }"
            @click="handleAddAllToCart"
            :disabled="addingAllItems"
          >
            {{ addingAllItems ? wishlist.table.adding_all_to_cart : wishlist.table.add_all_to_cart }}
          </button>
          <button
            v-if="selectedItems.length > 0"
            type="button"
            class="stwlite-tw-table-btn stwlite-tw-table-btn-add-to-cart-button stwlite-tw-add-selected-to-cart-btn"
            :style="{ fontSize: wishlist.pageStyles?.buttonTextSize + 'px', color: wishlist.pageStyles?.buttonTextColor, backgroundColor: wishlist.pageStyles?.buttonBackgroundColor }"
            @click="handleAddSelectedToCart"
            :disabled="addingSelectedItems"
          >
            {{ addingSelectedItems ? wishlist.table.adding_selected_to_cart : wishlist.table.add_selected_to_cart }}
          </button>
        </div>
      </div>
    </div>
    <div v-else class="stwlite-tw-table-wrapper">
      <div class="stwlite-tw-table-container">
        <table class="stwlite-tw-data-table">
          <tbody>
            <tr>
              <td>{{ wishlist.loading }}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
  <div v-else class="saveto-wishlist-empty-wrapper">
    <div class="saveto-wishlist-empty-container">
      <h2>{{ wishlist.empty.title }}</h2>
      <p class="stwlite-tw-subtitle">{{ wishlist.empty.description }}</p>
      <a :href="wishlist.pageUrls.myAccountUrl" class="stwlite-tw-wishlist-empty-action-btn">
        {{ wishlist.loginToView }}
      </a>
    </div>
  </div>

  ${confirmationModalTemplate({
    show: 'deleteConfirmPopup',
    title: 'wishlist.confirmDeleteTitle',
    message: 'wishlist.confirmRemoveItemMessage',
    onCancel: 'handleDeleteItemCancel',
    onConfirm: 'handleDeleteItemConfirm',
    cancelText: 'wishlist.cancel',
    confirmText: 'wishlist.table.delete',
    disabled: 'deletingItem',
  })}
</div>
`

export default templateGuest