"use strict";

exports.__esModule = true;
exports.default = exports.useRemoteSender = exports.RemoteSenderProvider = exports.RemoteSenderContext = exports.useRemoteSenderImpl = void 0;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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; }

const useRemoteSenderImpl = () => {
  const {
    subsonic
  } = (0, _hooks.useSubsonic)();
  const {
    receivers: receiverPLs = []
  } = (0, _hooks.usePlaylistsScanner)();
  const [receiverPL, setRecieverPL] = (0, _react.useState)(null);
  const receivers = receiverPLs.map(pl => ({
    name: pl.name.substring('zz-sbr-'.length),
    id: pl.id
  }));

  const setReceiver = id => {
    console.log('set receiver', id);

    if (!id) {
      setRecieverPL(r => null);
      return;
    }

    (async () => {
      const pl = await subsonic.getPlaylist(id);
      setRecieverPL(r => pl);
    })();
  };

  const _sendRemoteInstruction = instruction => {
    if (!receiverPL) return;
    console.log('sending instruction', instruction);

    (async () => {
      let pl = receiverPL;
      pl.comment = instruction;
      pl = await subsonic.updatePlaylist(pl.id, pl.comment, false, pl.name, null, null);
    })();
  };

  const sendRemoteRoute = route => {
    _sendRemoteInstruction('route ' + route);
  };

  const sendRemoteAction = action => {
    _sendRemoteInstruction('action ' + action);
  };

  return {
    setReceiver,
    isEnabled: !!receiverPL,
    receivers,
    receiver: receiverPL ? receiverPL.name.substring('zz-sbr-'.length) : null,
    sendRemoteRoute,
    sendRemoteAction
  };
};

exports.useRemoteSenderImpl = useRemoteSenderImpl;
const RemoteSenderContext = /*#__PURE__*/(0, _react.createContext)();
exports.RemoteSenderContext = RemoteSenderContext;

const RemoteSenderProvider = ({
  children
}) => /*#__PURE__*/_react.default.createElement(RemoteSenderContext.Provider, {
  value: useRemoteSenderImpl()
}, children);

exports.RemoteSenderProvider = RemoteSenderProvider;
RemoteSenderProvider.propTypes = {
  children: _propTypes.default.any
};

const useRemoteSender = () => (0, _react.useContext)(RemoteSenderContext);

exports.useRemoteSender = useRemoteSender;
var _default = useRemoteSender;
exports.default = _default;