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'],
    })
)

@@ -303,22 +303,16 @@
 default 
-async 
 function
@@ -345,64 +345,8 @@
 ) %7B%0A
-    const %7B rows %7D = await getURLMatchingDocs(%7B url %7D)%0A%0A
     
@@ -391,50 +391,39 @@
 ete 
-$%7Brows.length%7D matching records to 
+all data matching site:%5Cn
 $%7Burl%7D
-?
 %60)) 
@@ -436,24 +436,126 @@
     
-deleteDocs(rows)
+getURLMatchingDocs(%7B url %7D)%0A            .then(result =%3E deleteDocs(result.rows))%0A            .catch(f =%3E f) // Too bad
 %0A   

Remove docs count for user prompt