<?php

// Exit if accessed directly.
if (! defined('ABSPATH')) {
    exit;
}


$integrazo_wczo_integration_sources = [
    'plugins' => [
        ['name' => 'Woocommerce', 'id' => 'woocommerce', 'img' => 'woocommerce.png'],
    ],
    'products' => [
        ['name' => 'Zoho Bigin',        'id' => 'zoho_bigin',         'img' => 'zoho_bigin.png'],
        ['name' => 'Zoho Campaigns',     'id' => 'zoho_campaign',      'img' => 'zoho_campaign.png'],
        ['name' => 'Zoho Inventory',     'id' => 'zoho_inventory',      'img' => 'zoho_inventory.png'],
        ['name' => 'Zoho Books',     'id' => 'zoho_books',      'img' => 'zoho_books.png'],
        ['name' => 'Zoho Invoice',     'id' => 'zoho_invoice',      'img' => 'zoho_invoice.png'],
        ['name' => 'Zoho CRM',          'id' => 'zoho_crm',           'img' => 'zoho_crm.png'],
        ['name' => 'Zoho Desk',         'id' => 'zoho_desk',          'img' => 'zoho_desk.png'],
    ]
];


/**
 * $force_mode:
 * - 'auto'    => use flags (current behavior)
 * - 'grouped' => free A–Z then Pro A–Z (ignore flags)
 * - 'flat'    => all A–Z (ignore flags)
 */
function integrazo_wczo_get_plugins_sorted($plugins, $force_mode = 'auto')
{
    if (!is_array($plugins)) return [];

    $cmp = static function ($a, $b) {
        return strcasecmp($a['name'] ?? '', $b['name'] ?? '');
    };

    // Decide mode
    if ($force_mode === 'auto') {
        $pro_active  = function_exists('integrazo_wczo_is_pro_active') ? (bool) integrazo_wczo_is_pro_active() : false;
        $has_session = function_exists('integrazo_wczo_session')      ? (bool) integrazo_wczo_session()      : false;
        $mode = ($pro_active && $has_session) ? 'flat' : 'grouped';
    } else {
        $mode = $force_mode;
    }

    if ($mode === 'flat') {
        $all = array_values($plugins);
        usort($all, $cmp);
        return $all;
    }

    // grouped: free A–Z, then pro A–Z
    $free = [];
    $pro  = [];
    foreach ($plugins as $plg) {
        $is_pro = !empty($plg['pro']) && $plg['pro'];
        if ($is_pro) {
            $pro[] = $plg;
        } else {
            $free[] = $plg;
        }
    }
    usort($free, $cmp);
    usort($pro,  $cmp);
    return array_merge($free, $pro);
}

function integrazo_wczo_get_products_sorted($products, $force_mode = 'auto')
{
    if (!is_array($products)) return [];

    $cmp = static function ($a, $b) {
        return strcasecmp($a['name'] ?? '', $b['name'] ?? '');
    };

    // Decide mode
    if ($force_mode === 'auto') {
        $pro_active  = function_exists('integrazo_wczo_is_pro_active') ? (bool) integrazo_wczo_is_pro_active() : false;
        $has_session = function_exists('integrazo_wczo_session')      ? (bool) integrazo_wczo_session()      : false;
        $mode = ($pro_active && $has_session) ? 'flat' : 'grouped';
    } else {
        $mode = $force_mode;
    }

    if ($mode === 'flat') {
        $all = array_values($products);
        usort($all, $cmp);
        return $all;
    }

    // grouped: free A–Z, then pro A–Z
    $free = [];
    $pro  = [];
    foreach ($products as $prod) {
        $is_pro = !empty($prod['pro']) && $prod['pro'];
        if ($is_pro) {
            $pro[] = $prod;
        } else {
            $free[] = $prod;
        }
    }
    usort($free, $cmp);
    usort($pro,  $cmp);
    return array_merge($free, $pro);
}


/**
 * Return only Pro product IDs, sorted A–Z by product name.
 *
 * @param array $products The products array (e.g., $integrazo_wczo_integration_sources['products'])
 * @return array List of product IDs (strings) that are marked 'pro' => true
 */
function integrazo_wczo_get_pro_product_ids($products)
{
    if (!is_array($products)) return [];

    // Filter to pro-only
    $pro_only = array_filter($products, static function ($prod) {
        return !empty($prod['pro']);
    });

    // Sort A–Z by name (case-insensitive)
    usort($pro_only, static function ($a, $b) {
        return strcasecmp($a['name'] ?? '', $b['name'] ?? '');
    });

    // Return only IDs
    return array_values(array_map(static function ($prod) {
        return (string) ($prod['id'] ?? '');
    }, $pro_only));
}


/**
 * Return only Pro plugin IDs, sorted A–Z by plugin name.
 *
 * @param array $plugins The plugins array (e.g., $integrazo_wczo_integration_sources['plugins'])
 * @return array List of plugin IDs (strings) that are marked 'pro' => true
 */
function integrazo_wczo_get_pro_plugin_ids($plugins)
{
    if (!is_array($plugins)) return [];

    // Filter to pro-only
    $pro_only = array_filter($plugins, static function ($plugin) {
        return !empty($plugin['pro']);
    });

    // Sort A–Z by name (case-insensitive)
    usort($pro_only, static function ($a, $b) {
        return strcasecmp($a['name'] ?? '', $b['name'] ?? '');
    });

    // Return only IDs
    return array_values(array_map(static function ($plugin) {
        return (string) ($plugin['id'] ?? '');
    }, $pro_only));
}
