"use strict";

exports.__esModule = true;
exports.RemoteSenderMenuButton = exports.default = exports.RemoteSenderMenu = void 0;

var _react = _interopRequireWildcard(require("react"));

var _IconButton = _interopRequireDefault(require("@material-ui/core/IconButton"));

var _Menu = _interopRequireDefault(require("@material-ui/core/Menu"));

var _MenuItem = _interopRequireDefault(require("@material-ui/core/MenuItem"));

var _ListItem = _interopRequireDefault(require("@material-ui/core/ListItem"));

var _ListItemIcon = _interopRequireDefault(require("@material-ui/core/ListItemIcon"));

var _ListItemText = _interopRequireDefault(require("@material-ui/core/ListItemText"));

var _styles = require("@material-ui/core/styles");

var _mdiMaterialUi = require("mdi-material-ui");

var _hooks = require("subfirelib/hooks");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const ITEM_HEIGHT = 48;

const styles = theme => ({
  listItemReceiver: {
    display: 'none'
  },
  receiver: {
    display: 'none'
  },
  sending: {
    color: theme.palette.primary.light
  },
  normal: {
    color: '#999999'
  }
});

const RemoteSenderMenu = props => {
  const {
    setReceiver,
    isEnabled,
    receivers,
    receiver
  } = props;
  const [anchorEl, setAnchorEL] = (0, _react.useState)(null);

  const handleClick = event => {
    setAnchorEL(e => event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEL(e => null);
  };

  const handleMenuClick = playlist => {
    console.log('playlist', playlist);
    setReceiver(playlist.id);
    setAnchorEL(e => null);
  };

  const handleDisableClick = () => {
    setReceiver(null);
  };

  const {
    classes,
    variant = 'button'
  } = props;
  const options = receivers;
  const isReceiver = false; // getting rid of this

  const isSending = isEnabled; // console.warn('props', props);

  let className, liClassName;

  if (isReceiver) {
    className = classes.receiver;
    liClassName = classes.listItemReceiver;
  } else if (isSending) {
    className = classes.sending;
  } else if (options.length === 0) {
    className = classes.receiver; // nobody to see

    liClassName = classes.listItemReceiver;
  } else {
    className = classes.normal;
  }

  const TheIcon = isSending ? _mdiMaterialUi.RemoteTv : _mdiMaterialUi.RemoteTvOff;
  let rootOn, rootOff;

  if (variant === 'button') {
    rootOn = /*#__PURE__*/_react.default.createElement(_IconButton.default, {
      className: className,
      onClick: handleDisableClick
    }, /*#__PURE__*/_react.default.createElement(TheIcon, null));
    rootOff = /*#__PURE__*/_react.default.createElement(_IconButton.default, {
      "aria-label": "show list",
      "aria-owns": anchorEl ? 'long-menu' : null,
      "aria-haspopup": "true",
      className: className,
      onClick: handleClick
    }, /*#__PURE__*/_react.default.createElement(TheIcon, null));
  } else if (variant === 'listItem') {
    rootOn = /*#__PURE__*/_react.default.createElement(_ListItem.default, {
      className: liClassName,
      button: true,
      onClick: handleDisableClick
    }, /*#__PURE__*/_react.default.createElement(_ListItemIcon.default, {
      className: className
    }, /*#__PURE__*/_react.default.createElement(TheIcon, null)), /*#__PURE__*/_react.default.createElement(_ListItemText.default, {
      primary: "Remote Sending On"
    }));
    rootOff = /*#__PURE__*/_react.default.createElement(_ListItem.default, {
      className: liClassName,
      button: true,
      "aria-label": "show list",
      "aria-owns": anchorEl ? 'long-menu' : null,
      "aria-haspopup": "true",
      onClick: handleClick
    }, /*#__PURE__*/_react.default.createElement(_ListItemIcon.default, {
      className: className
    }, /*#__PURE__*/_react.default.createElement(TheIcon, null)), /*#__PURE__*/_react.default.createElement(_ListItemText.default, {
      primary: "Remote Sending Off"
    }));
  }

  if (isSending) {
    return rootOn;
  } // console.warn(options);


  return /*#__PURE__*/_react.default.createElement("div", null, rootOff, /*#__PURE__*/_react.default.createElement(_Menu.default, {
    id: "long-menu",
    anchorEl: anchorEl,
    open: Boolean(anchorEl),
    onClose: handleClose,
    PaperProps: {
      style: {
        maxHeight: ITEM_HEIGHT * 4.5,
        width: 200
      }
    }
  }, options.map(option => /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
    key: option.id,
    onClick: handleMenuClick.bind(void 0, option)
  }, option.name))));
};

exports.RemoteSenderMenu = RemoteSenderMenu;
const RemoteSenterMenuStyled = (0, _styles.withStyles)(styles)(RemoteSenderMenu);
var _default = RemoteSenterMenuStyled;
exports.default = _default;

const RemoteSenderMenuButton = props => {
  const rsProps = (0, _hooks.useRemoteSender)();
  return /*#__PURE__*/_react.default.createElement(RemoteSenterMenuStyled, _extends({
    variant: "button"
  }, rsProps));
};

exports.RemoteSenderMenuButton = RemoteSenderMenuButton;