<?php

namespace WeDevs\DokanPro\Modules\Germanized;

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

/**
 * Teaches Dokan how to render Germanized's withdrawal order statuses on the vendor dashboard.
 *
 * Germanized registers two extra *order* statuses — `pending-wdraw` ("Pending withdrawal") and
 * `withdrawn` ("Withdrawn"). Dokan renders the vendor-facing status through
 * `dokan_get_order_status_translated()` and `dokan_get_order_status_class()`, both of which switch
 * over WooCommerce's core statuses only and fall through to an empty string for anything else — so
 * an order sitting in either withdrawal status shows a blank status on the vendor order list and
 * order-details pages, even though WooCommerce itself knows the label.
 *
 * Both helpers end in a filter, which is what this class hooks. The label is read back from
 * WooCommerce's own status registry rather than duplicated here, so it stays in sync with whatever
 * Germanized registers and needs no translation strings of its own.
 *
 * @since 5.0.12
 */
class WithdrawalOrderStatus {

    /**
     * Germanized order statuses (unprefixed) mapped to the Dokan label class they render with.
     *
     * `pending-wdraw` is an open request awaiting the vendor, so it reads as a warning; `withdrawn`
     * is a settled outcome and reads neutral, the same way Dokan renders refunded and cancelled.
     */
    const STATUS_CLASSES = [
        'pending-wdraw' => 'warning',
        'withdrawn'     => 'default',
    ];

    /**
     * Constructor.
     *
     * @since 5.0.12
     */
    public function __construct() {
        add_filter( 'dokan_get_order_status_translated', [ $this, 'translate_status' ], 10, 2 );
        add_filter( 'dokan_get_order_status_class', [ $this, 'status_class' ], 10, 2 );
    }

    /**
     * Supply the label Dokan could not resolve for a withdrawal order status.
     *
     * @since 5.0.12
     *
     * @param string $translated Label Dokan resolved; empty for statuses it does not know.
     * @param string $status     Order status, with or without the `wc-` prefix.
     *
     * @return string
     */
    public function translate_status( $translated, $status ) {
        if ( '' !== (string) $translated || ! $this->is_withdrawal_status( $status ) ) {
            return $translated;
        }

        return wc_get_order_status_name( $status );
    }

    /**
     * Supply the label class Dokan could not resolve for a withdrawal order status.
     *
     * Without this the status renders as an unstyled `dokan-label dokan-label-` element.
     *
     * @since 5.0.12
     *
     * @param string $status_class Class Dokan resolved; empty for statuses it does not know.
     * @param string $status       Order status, with or without the `wc-` prefix.
     *
     * @return string
     */
    public function status_class( $status_class, $status ) {
        if ( '' !== (string) $status_class || ! $this->is_withdrawal_status( $status ) ) {
            return $status_class;
        }

        return self::STATUS_CLASSES[ Helper::unprefix_order_status( $status ) ];
    }

    /**
     * Whether the given status is one of Germanized's withdrawal order statuses, and still one
     * WooCommerce recognises.
     *
     * The second half matters because Germanized can be deactivated with orders left sitting in
     * one of its statuses. WooCommerce then no longer knows the label and `wc_get_order_status_name()`
     * hands back the raw slug, so without this check the vendor would read "pending-wdraw" where
     * they previously read nothing at all.
     *
     * @since 5.0.12
     *
     * @param string $status Order status, with or without the `wc-` prefix.
     *
     * @return bool
     */
    protected function is_withdrawal_status( $status ) {
        $status = Helper::unprefix_order_status( $status );

        if ( ! isset( self::STATUS_CLASSES[ $status ] ) ) {
            return false;
        }

        return array_key_exists( 'wc-' . $status, wc_get_order_statuses() );
    }
}
