import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

import React, { useEffect, useImperativeHandle, useRef, useState } from "react";
import Checkbox from "../../ui/next/Checkbox";
import Collapse from "../../ui/Collapse";
import CollapseTrigger from "../../ui/CollapseTrigger";
import CssFlex from "../../ui/CssFlex";
import Typography from "../../ui/Typography";

function PrivacyCheckboxes(_ref, ref) {
  var onChange = _ref.onChange,
      translations = _ref.translations;

  var _useState = useState(true),
      _useState2 = _slicedToArray(_useState, 2),
      open = _useState2[0],
      setOpen = _useState2[1];

  var _useState3 = useState({
    obligatory: false,
    marketing_cleverads: false,
    marketing_third_party: false,
    shared_data: false
  }),
      _useState4 = _slicedToArray(_useState3, 2),
      checkboxesState = _useState4[0],
      setCheckboxesState = _useState4[1];

  var obligatoryRef = useRef(null); // This allows to call a validate method from the parent through the ref:
  // Example:
  // const ref = useRef();
  //
  // const handleSubmit = () => {
  //    ref.current.validate();
  // };
  // ...
  // <PrivacyCheckboxes ref={ref} />
  //

  useImperativeHandle(ref, function () {
    return {
      validate: function validate() {
        var _obligatoryRef$curren;

        if (!checkboxesState.obligatory) {
          setOpen(true);
        } // It will show the native message if the checkbox is not checked
        // It also returns a boolean indicating if the element is valid


        return (_obligatoryRef$curren = obligatoryRef.current) === null || _obligatoryRef$curren === void 0 ? void 0 : _obligatoryRef$curren.reportValidity();
      }
    };
  });
  var allChecked = Object.values(checkboxesState).every(Boolean);
  var someCheckedButNotAll = Object.values(checkboxesState).some(Boolean) && !allChecked;

  var handleAllChecked = function handleAllChecked() {
    var newValue = !allChecked;
    setCheckboxesState({
      obligatory: newValue,
      marketing_cleverads: newValue,
      marketing_third_party: newValue,
      shared_data: newValue
    });
  };

  var handleCheckboxToggle = function handleCheckboxToggle(key) {
    setCheckboxesState(_objectSpread(_objectSpread({}, checkboxesState), {}, _defineProperty({}, key, !checkboxesState[key])));
  }; // Sync the state with the parent


  useEffect(function () {
    onChange(checkboxesState);
  }, [checkboxesState, onChange]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CssFlex, {
    gap: "sm"
  }, /*#__PURE__*/React.createElement(Checkbox, {
    checked: allChecked,
    indeterminate: someCheckedButNotAll,
    onChange: handleAllChecked
  }, /*#__PURE__*/React.createElement(Typography, {
    component: "span",
    color: "neutral",
    weight: "semibold",
    shade: 800
  }, translations.acceptAll)), /*#__PURE__*/React.createElement(CollapseTrigger, {
    open: open,
    onClick: function onClick() {
      return setOpen(!open);
    }
  })), /*#__PURE__*/React.createElement(Collapse, {
    open: open
  }, /*#__PURE__*/React.createElement(CssFlex, {
    direction: "column",
    gap: "md",
    mt: "md",
    ml: "xl"
  }, /*#__PURE__*/React.createElement(Checkbox, {
    ref: obligatoryRef,
    required: true,
    checked: checkboxesState.obligatory,
    onChange: function onChange() {
      return handleCheckboxToggle("obligatory");
    }
  }, translations.obligatory), /*#__PURE__*/React.createElement(Checkbox, {
    checked: checkboxesState.marketing_cleverads,
    onChange: function onChange() {
      return handleCheckboxToggle("marketing_cleverads");
    }
  }, translations.marketing_cleverads), /*#__PURE__*/React.createElement(Checkbox, {
    checked: checkboxesState.marketing_third_party,
    onChange: function onChange() {
      return handleCheckboxToggle("marketing_third_party");
    }
  }, translations.marketing_third_party), /*#__PURE__*/React.createElement(Checkbox, {
    checked: checkboxesState.shared_data,
    onChange: function onChange() {
      return handleCheckboxToggle("shared_data");
    }
  }, translations.shared_data))));
}

export default /*#__PURE__*/React.forwardRef(PrivacyCheckboxes);