import db, { normaliseFindResult } from 'src/pouchdb'
import { deleteDocs } from 'src/page-storage/deletion'
import { addToBlacklist } from '..'

/**
 * Handles confirmation and running of a quick blacklist request from the popup script.
 *
 * @param {string} url The URL being blacklisted.
 */
export default async function quickBlacklistConfirm(url) {
    const { rows } = await getURLMatchingDocs({ url })

    if (window.confirm(`Do you want to delete ${rows.length} matching records to ${url}?`)) {
        deleteDocs(rows)
    }

    addToBlacklist(url)
}

/**
 * Get all docs with matching URLs.
 *
 * @param {string} {url} Value to use to match against doc URLs.
 */
const getURLMatchingDocs = async ({ url, selector = {} }) => normaliseFindResult(
    await db.find({
        selector: {
            ...selector,
            url: { $regex: url }, // All docs whose URLs contain this value
        },
        fields: ['_id', '_rev'],
    })
)

import db, { normaliseFindResult } from 'src/pouchdb'
import { deleteDocs } from 'src/page-storage/deletion'
import { addToBlacklist } from '..'

/**
 * Handles confirmation and running of a quick blacklist request from the popup script.
 *
 * @param {string} url The URL being blacklisted.
 */
export default function quickBlacklistConfirm(url) {
    if (window.confirm(`Do you want to delete all data matching site:\n${url}`)) {
        getURLMatchingDocs({ url })
            .then(result => deleteDocs(result.rows))
            .catch(f => f) // Too bad
    }

    addToBlacklist(url)
}

/**
 * Get all docs with matching URLs.
 *
 * @param {string} {url} Value to use to match against doc URLs.
 */
const getURLMatchingDocs = async ({ url, selector = {} }) => normaliseFindResult(
    await db.find({
        selector: {
            ...selector,
            url: { $regex: url }, // All docs whose URLs contain this value
        },
        fields: ['_id', '_rev'],
    })
)

Remove docs count for user prompt
Remove docs count for user prompt

- it simply took too long to query the DB for when the DB is more full (30+ secs)
- put indexes on, however would only with with '_id' + 'url', and then querying per doc type ended up taking minutes even with index :/
- now do it async in the background and remove the count given to user
