<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_View_Data_Label' ) ) {

    class Pricely_View_Data_Label {

        private static $instance;

        public static function get_instance(): self {

            if ( !self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function get_data( $view_data ) {

            if ( !$this->can_process_view( $view_data ) ) {

                return false;
            }

            $label_args = $view_data[ 'label_args' ];

            $label_mode = $label_args[ 'mode' ];

            if ( 'text' == $label_mode ) {

                return $this->process_label_data( $this->get_text_data( $label_args ), $view_data );
            }

            return false;
        }

        private function process_label_data( $label_data, $view_data ) {

            $label_data[ 'ui_css_class' ] = $view_data[ 'ui_css_class' ];

            return $label_data;
        }

        private function get_text_data( $label_args ) {

            return $this->process_data( array(), $label_args[ 'text' ], 0 );
        }

        private function process_data( $label_data, $label_text, $variation_id ) {

            if ( !isset( $label_data[ 'icon' ] ) ) {

                $label_data[ 'icon' ] = false;
            }

            $label_data[ 'labels' ][ $variation_id ] = $label_text;

            return $label_data;
        }

        private function can_process_view( $view_data ) {

            if ( !$view_data[ 'label_args' ] ) {

                return false;
            }

            return true;
        }

    }

}

