<?php
namespace SabaiApps\Directories\Component\Social\CSVImporter;

use SabaiApps\Directories\Component\CSV\Importer\AbstractImporter;
use SabaiApps\Directories\Component\CSV\Importer\IWpAllImportImporter;
use SabaiApps\Directories\Component\Entity\Model\Field;

class SocialCSVImporter extends AbstractImporter implements IWpAllImportImporter {

	public function csvImporterColumns( Field $field ) {
		$columns = array();
		foreach ( $this->_application->Social_Medias() as $media_name => $media ) {
			$columns[ $media_name ] = $media['label'];
		}
		return $columns;
	}

	public function csvWpAllImportImporterAddField( \RapidAddon $addon, Field $field ) {
		$addon->add_title( $field->getFieldLabel() );
		foreach ( $this->_application->Social_Medias() as $media_name => $media ) {
			$addon->add_field(
				$field->getFieldName() . '-' . $media_name,
				$media['label'],
				'text',
				null,
				! isset( $media['type'] ) ? __( 'Enter a URL', 'directories-pro' ) : ''
			);
		}
		return true;
	}

	public function csvWpAllImportImporterDoImport( \RapidAddon $addon, Field $field, array $data, $options, array $article ) {
		$values = array();
		foreach ( $this->_application->Social_Medias() as $media_name => $media ) {
			if ( ! isset( $data[ $field->getFieldName() . '-' . $media_name ] )
				|| ( ! $value = trim( $data[ $field->getFieldName() . '-' . $media_name ] ) )
			) {
				continue;
			}

			$values[ $media_name ] = $value;
		}
		return array( $values );
	}
}
