/**
 * @file
 * Manages the nodehive_sidebar_space cookie based on current page context.
 */

(function (Drupal, drupalSettings, once) {
  'use strict';

  var COOKIE_NAME = 'nodehive_sidebar_space';
  var COOKIE_MAX_AGE = 31536000; // 1 year

  Drupal.behaviors.nodehiveSidebarSpaceCookie = {
    attach: function (context) {
      once('nh-sidebar-space-cookie', 'body', context).forEach(function () {
        var settings = drupalSettings.nodehiveSidebarCookie || {};

        if (settings.setSpaceId) {
          document.cookie = COOKIE_NAME + '=' + settings.setSpaceId + ';path=/;max-age=' + COOKIE_MAX_AGE + ';SameSite=Lax';
        }

        if (settings.clearCookie) {
          document.cookie = COOKIE_NAME + '=;path=/;max-age=0;SameSite=Lax';
        }
      });
    }
  };

})(Drupal, drupalSettings, once);
