// Instagram profil sayfasında tarayıcı konsolunda çalıştırılacak kod
// UYARI: Bu kod sadece eğitim amaçlıdır. Instagram'ın kullanım şartlarını ihlal eder.

async function deleteInstagramPost() {
    const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
    
    try {
        console.log("Gönderi silme işlemi başlatılıyor...");
        
        // 1. İlk gönderiye tıkla (reel veya post)
        const firstPost = document.querySelector('a[href*="/p/"], a[href*="/reel/"]');
        if (!firstPost) {
            console.error("Gönderi bulunamadı! Tüm gönderiler silinmiş olabilir.");
            return false;
        }
        firstPost.click();
        console.log("Gönderiye tıklandı...");
        await delay(2500);
        
        // 2. Üç nokta menü butonunu bul ve tıkla
        const menuButton = [...document.querySelectorAll('button, div[role="button"]')].find(btn => {
            const svg = btn.querySelector('svg[aria-label*="Diğer"]') || 
                       btn.querySelector('svg[aria-label*="More"]') ||
                       btn.querySelector('svg[aria-label*="seçenekler"]');
            return svg !== null;
        });
        
        if (!menuButton) {
            console.error("Menü butonu bulunamadı!");
            return false;
        }
        menuButton.click();
        console.log("Menü açıldı...");
        await delay(1500);
        
        // 3. "Sil" butonunu bul ve tıkla
        const deleteButton = [...document.querySelectorAll('button')].find(btn => 
            btn.textContent.trim() === 'Sil' || btn.textContent.trim() === 'Delete'
        );
        
        if (!deleteButton) {
            console.error("Sil butonu bulunamadı!");
            return false;
        }
        deleteButton.click();
        console.log("Sil butonuna tıklandı...");
        await delay(1500);
        
        // 4. Silme işlemini onayla
        const confirmButton = [...document.querySelectorAll('button')].find(btn => 
            btn.textContent.trim() === 'Sil' || btn.textContent.trim() === 'Delete'
        );
        
        if (!confirmButton) {
            console.error("Onay butonu bulunamadı!");
            return false;
        }
        confirmButton.click();
        console.log("Silme işlemi onaylandı!");
        
        await delay(2000);
        console.log("✅ Gönderi başarıyla silindi!");
        return true;
        
    } catch (error) {
        console.error("Hata oluştu:", error);
        return false;
    }
}

// Otomatik döngü fonksiyonu
let deleteInterval;
let deletedCount = 0;

async function startAutoDelete(intervalSeconds = 10, maxPosts = 10) {
    console.log(`🚀 Otomatik silme başlatıldı!`);
    console.log(`⏱️ Aralık: ${intervalSeconds} saniye`);
    console.log(`📊 Maksimum: ${maxPosts} gönderi`);
    console.log(`⚠️ Durdurmak için: stopAutoDelete()`);
    console.log("=".repeat(50));
    
    // İlk gönderiyi hemen sil
    const success = await deleteInstagramPost();
    if (success) deletedCount++;
    
    // Belirtilen aralıklarla devam et
    deleteInterval = setInterval(async () => {
        if (deletedCount >= maxPosts) {
            console.log(`✅ ${maxPosts} gönderi silindi. İşlem tamamlandı!`);
            stopAutoDelete();
            return;
        }
        
        // Profil sayfasına geri dön
        const currentUrl = window.location.href;
        if (currentUrl.includes('/p/') || currentUrl.includes('/reel/')) {
            window.history.back();
            await new Promise(r => setTimeout(r, 2000));
        }
        
        const success = await deleteInstagramPost();
        if (success) {
            deletedCount++;
            console.log(`📈 Silinen gönderi sayısı: ${deletedCount}/${maxPosts}`);
        } else {
            console.log("⚠️ Gönderi silinemedi, döngü durduruluyor...");
            stopAutoDelete();
        }
    }, intervalSeconds * 1000);
}

function stopAutoDelete() {
    if (deleteInterval) {
        clearInterval(deleteInterval);
        deleteInterval = null;
        console.log("🛑 Otomatik silme durduruldu!");
        console.log(`📊 Toplam silinen: ${deletedCount} gönderi`);
        deletedCount = 0;
    } else {
        console.log("ℹ️ Zaten çalışan bir işlem yok.");
    }
}

// KULLANIM:
// startAutoDelete(10, 5);  // 10 saniyede bir, toplam 5 gönderi sil
// stopAutoDelete();         // Durdurmak için

console.log("✅ Kodlar yüklendi!");
console.log("📝 Kullanım: startAutoDelete(saniye, adet)");
console.log("Örnek: startAutoDelete(10, 5) - Her 10 saniyede bir, 5 gönderi sil");