<?php

namespace WeDevs\DokanPro\Modules\VendorSupport;

use WeDevs\Dokan\Admin\Dashboard\Pages\AbstractPage;
use WeDevs\DokanPro\Modules\VendorSupport\Models\Ticket;

class AdminMenu extends AbstractPage {

    /**
     * Menu id.
     *
     * @since 4.1.2
     *
     * @return string
     */
    public function get_id(): string {
        return 'vendor-support';
    }

    /**
     * Menu name.
     *
     * @since 4.1.2
     *
     * @param string $capability
     * @param string $position
     *
     * @return array
     */
    public function menu( string $capability, string $position ): array {
        $pending_count = Ticket::count_pending();

        $menu_title = $pending_count ? sprintf(
            /* translators: %s: Pending support ticket count badge */
            __( 'Vendor Support %s', 'dokan' ),
            '<span class="awaiting-mod count-1"><span class="pending-count">'
            . number_format_i18n( $pending_count )
            . '</span></span>'
        ) : esc_html__( 'Vendor Support', 'dokan' );

        return [
            'page_title' => __( 'Dokan Vendor Support', 'dokan' ),
            'menu_title' => $menu_title,
            'route'      => 'vendor-support',
            'capability' => $capability,
            'position'   => 99,
        ];
    }

    /**
     * Menu settings.
     *
     * @since 4.1.2
     *
     * @return array|mixed[]
     */
    public function settings(): array {
        return [];
    }

    /**
     * Script handles.
     *
     * @since 4.1.2
     *
     * @return string[]
     */
    public function scripts(): array {
        return [ 'dokan-vendor-support' ];
    }

    /**
     * Style handles.
     *
     * @since 4.1.2
     *
     * @return string[]
     */
    public function styles(): array {
        return [ 'style-dokan-vendor-support' ];
    }

    /**
     * Register the script.
     *
     * @since 4.1.2
     *
     * @return void
     */
    public function register(): void {
        $asset_file = DOKAN_VENDOR_SUPPORT_DIR . '/assets/js/admin-vendor-support.asset.php';
        if ( ! file_exists( $asset_file ) ) {
            return;
        }
        $asset = include $asset_file;

        wp_register_script(
            'dokan-vendor-support',
            DOKAN_VENDOR_SUPPORT_ASSETS . '/js/admin-vendor-support.js',
            array_merge( $asset['dependencies'], [ 'moment', 'dokan-util-helper', 'dokan-accounting', 'dokan-react-components', 'wc-components' ] ),
            $asset['version'],
            [
                'strategy' => 'defer',
                'in_footer' => true,
            ]
        );

        wp_register_style( 'style-dokan-vendor-support', DOKAN_VENDOR_SUPPORT_ASSETS . '/js/style-admin-vendor-support.css', [ 'wp-components', 'wc-components', 'dokan-react-components' ], $asset['version'] );
    }
}
