<?php
/**
 * @author 1902 Software
 * @copyright Copyright © 2023 1902 Software (https://1902software.com/magento/)
 * @package WriteTextAI_WriteTextAI
 */
?>

<?php
/** @var \WriteTextAI\WriteTextAI\ViewModel\PipelineSettings $pipelineSettingsViewModel */
/** @var \WriteTextAI\WriteTextAI\ViewModel\Timezone $timezone */

$pipelineSettingsViewModel = $block->getPipelineSettings();
$timezone = $block->getTimezone();
$automaticTextOptimization = $pipelineSettingsViewModel->getAutomaticTextOptimization();
$rules = $pipelineSettingsViewModel->getRules();
$minTriggerActionNPosition = $rules['minTriggerActionNPosition'] ?? 2;
$maxTriggerActionNPosition = $rules['maxTriggerActionNPosition'] ?? 100;
$defaultTriggerActionNPosition = $pipelineSettingsViewModel->getTriggerActionNPositionDefaultValue();
$minTriggerActionNDays = $rules['minTriggerActionNDays'] ?? 1;
$maxTriggerActionNDays = $rules['maxTriggerActionNDays'] ?? 365;
$defaultTriggerActionNDays = $pipelineSettingsViewModel->getTriggerActionNDaysDefaultValue();
$defaultAutomaticTextOptimization = $pipelineSettingsViewModel->getDefaultAutomaticTextOptimization();

$defaultTriggerActionNPositionLow =
    $pipelineSettingsViewModel->getTriggerActionNPositionDefaultValuePerDifficulty('low');
$defaultTriggerActionNPositionLowMid =
    $pipelineSettingsViewModel->getTriggerActionNPositionDefaultValuePerDifficulty('low_mid');
$defaultTriggerActionNPositionMid =
    $pipelineSettingsViewModel->getTriggerActionNPositionDefaultValuePerDifficulty('mid');
$defaultTriggerActionNPositionMidHigh =
    $pipelineSettingsViewModel->getTriggerActionNPositionDefaultValuePerDifficulty('mid_high');
$defaultTriggerActionNDaysLow = $pipelineSettingsViewModel->getTriggerActionNDaysDefaultValuePerDifficulty('low');
$defaultTriggerActionNDaysLowMid =
    $pipelineSettingsViewModel->getTriggerActionNDaysDefaultValuePerDifficulty('low_mid');
$defaultTriggerActionNDaysMid = $pipelineSettingsViewModel->getTriggerActionNDaysDefaultValuePerDifficulty('mid');
$defaultTriggerActionNDaysMidHigh =
    $pipelineSettingsViewModel->getTriggerActionNDaysDefaultValuePerDifficulty('mid_high');

$entityType = $block->getEntityType();
$textFieldMapping = $pipelineSettingsViewModel->getTextFieldsMapping($entityType);
$isCustomOpenGraphEnabled = $pipelineSettingsViewModel->isCustomOpenGraphEnabled();
// phpcs:disable Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
?>

<div class="wtai-pipelines-content" data-bind="scope: 'wtaiKeywordPipelines'">
    <div class="wtai-difficulty-levels">
        <div class="wtai-difficulty-levels-wrapper">
            <div class="wtai-difficulty-levels-item"><?= $block->escapeHtml(__('HIGH')) ?></div>
            <div class="wtai-difficulty-levels-item"><?= $block->escapeHtml(__('MID HIGH')) ?></div>
            <div class="wtai-difficulty-levels-item"><?= $block->escapeHtml(__('MID')) ?></div>
            <div class="wtai-difficulty-levels-item"><?= $block->escapeHtml(__('LOW MID')) ?></div>
            <div class="wtai-difficulty-levels-item"><?= $block->escapeHtml(__('LOW')) ?></div>
        </div>
    </div> 
    <div class="wtai-clusterpipeline-container" data-bind="visible: pipelines().length > 0">
        <div class="wtai-clusterpipeline-label">
            <span class="wtai-clusterpipeline-label-start"></span>
            <span class="wtai-clusterpipeline-label-text">
                <?= $block->escapeHtml(__('CLUSTER-BASED PIPELINES')) ?>
            </span>
            <span class="wtai-clusterpipeline-label-end"></span>
        </div>
        <div class="wtai-pipelines" data-bind="
            foreach: { data: pipelines, as: '$pipeline' }
        ">
            <?= $block->getChildHtml('wtai.pipeline') ?>
        </div>
    </div>
    <div class="wtai-pipelines wtai-custom-pipeline"  data-bind="
        foreach: { data: customPipelines, as: '$pipeline' },
        afterRender: afterRenderCommon
    ">
        <?= $block->getChildHtml('wtai.pipeline') ?>
    </div>
    <?= $block->getChildHtml('wtai.edit.pipeline') ?>
    <?= $block->getChildHtml('wtai.empty.pipeline') ?>
    <?= $block->getChildHtml('wtai.keyword.ranking') ?>
</div>

<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "wtaiKeywordPipelines": {
                        "component": "<?= $block->escapeJs($block->getJsComponent()) ?>",
                        "entityType": "<?= $block->escapeJs($block->getEntityType()) ?>",
                        "getOptimizationUrl": "<?= $block->escapeJs(
                            $block->getUrl('wtai/edit/keywordsOptimization_get')
                        ) ?>",
                        "maxNumberOfKeywordPipeline": <?= /* @noEscape */ intval($rules['maxKeywords'] ?? 6) ?>,
                        "rules": <?= /* @noEscape */ json_encode($rules) ?>,
                        "triggerActionNPosition": <?= /* @noEscape */ $defaultTriggerActionNPosition ?>,
                        "minTriggerActionNPosition": <?= /* @noEscape */ $minTriggerActionNPosition ?>,
                        "maxTriggerActionNPosition": <?= /* @noEscape */ $maxTriggerActionNPosition ?>,
                        "triggerActionAfterNDays": <?= /* @noEscape */ $defaultTriggerActionNDays ?>,
                        "minTriggerActionNDays": <?= /* @noEscape */ $minTriggerActionNDays ?>,
                        "maxTriggerActionNDays": <?= /* @noEscape */ $maxTriggerActionNDays ?>,
                        "triggerActionNPositionLow": <?= /* @noEscape */ $defaultTriggerActionNPositionLow ?>,
                        "triggerActionNPositionLowMid": <?= /* @noEscape */ $defaultTriggerActionNPositionLowMid ?>,
                        "triggerActionNPositionMid": <?= /* @noEscape */ $defaultTriggerActionNPositionMid ?>,
                        "triggerActionNPositionMidHigh": <?= /* @noEscape */ $defaultTriggerActionNPositionMidHigh ?>,
                        "triggerActionNDaysLow": <?= /* @noEscape */ $defaultTriggerActionNDaysLow ?>,
                        "triggerActionNDaysLowMid": <?= /* @noEscape */ $defaultTriggerActionNDaysLowMid ?>,
                        "triggerActionNDaysMid": <?= /* @noEscape */ $defaultTriggerActionNDaysMid ?>,
                        "triggerActionNDaysMidHigh": <?= /* @noEscape */ $defaultTriggerActionNDaysMidHigh ?>,
                        "automaticTextOptimization": "<?= $block->escapeJs($defaultAutomaticTextOptimization) ?>",
                        "textFieldMapping": <?= /* @noEscape */ json_encode($textFieldMapping) ?>,
                        "isCustomOpenGraphEnabled": <?= /* @noEscape */ json_encode($isCustomOpenGraphEnabled) ?>,
                        "saveEditPipelineUrl": "<?= /* @noEscape */ $block->getUrl('wtai/edit/pipelines_save') ?>",
                        "addKeywordsToPoolUrl": "<?= /* @noEscape */ $block->getUrl('wtai/edit/keywordsPool_add') ?>",
                        "deletePipelineUrl": "<?= /* @noEscape */ $block->getUrl('wtai/edit/pipelines_delete') ?>",
                        "restorePreviousPipelineUrl":
                            "<?= /* @noEscape */ $block->getUrl('wtai/edit/pipelines_restore') ?>",
                        "lockPipelineUrl": "<?= /* @noEscape */ $block->getUrl('wtai/edit/pipelines_lock') ?>",
                        "unlockPipelineUrl": "<?= /* @noEscape */ $block->getUrl('wtai/edit/pipelines_unlock') ?>",
                        "applyTriggerActionUrl": "<?= /* @noEscape */ $block->getUrl('wtai/edit/triggers_update') ?>",
                        "applyTriggerActionMultipleUrl":
                            "<?= /* @noEscape */ $block->getUrl('wtai/edit/triggers_updateMultiple') ?>",
                        "timezone": "<?= $block->escapeJs($timezone->getTimezone()) ?>"
                    }
                }
            }
        }
    }
</script>
<?php
// phpcs:enable Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
?>