'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _printTools = require('../utils/print-tools');

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

const renderStatus = object => {
  const { status, type } = object.getStatus();
  switch (type) {
    case 'ok':
      return _chalk2.default.dim.green(status);
    case 'warn':
      return _chalk2.default.dim.yellow(status);
    case 'fail':
      return _chalk2.default.dim.red(status);
    default:
      return status;
  }
};

const socketListResponses = session => ({
  lackSockets: 'No Socket was found on server nor in config!',
  lackSocket: ({ name }) => `${name ? _chalk2.default.yellow(name) : 'This Socket'} was not found on server nor in config!`,
  createNewOne: `Type ${_chalk2.default.cyan('syncano-cli create <name>')} to create new one.`,
  installNewOne: `Type ${_chalk2.default.cyan('syncano-cli add <name>')} to install new one from registry.`,
  params: `${_chalk2.default.white('params')}:`,
  socket: socket => {
    let description = '';
    if (socket.existRemotely) {
      description = socket.remote.spec.description || '';
    } else {
      description = socket.spec.description || '';
    }

    return {
      name: `${_chalk2.default.blue.bold('socket')}: ${_chalk2.default.blue.bold(socket.name)}`,
      description: `${_chalk2.default.dim('description')}: ${description}`,
      version: `${_chalk2.default.dim('version')}: ${socket.version || ''}`,
      type: `${_chalk2.default.dim('type')}: ${socket.getType().msg}`,
      status: `${_chalk2.default.dim('status')}: ${renderStatus(socket)}`
    };
  },
  endpoint: endpoint => {
    const metadata = endpoint.metadata;

    return endpoint && {
      name: `${_chalk2.default.white('endpoint')}: ${_chalk2.default.cyan(endpoint.getFullName())}`,
      description: `${_chalk2.default.dim('description')}: ${metadata.description || ''}`,
      url: `${_chalk2.default.dim('url')}: ${endpoint.getURL()}`,
      notSynced: `${_chalk2.default.dim('status')}: ${renderStatus(endpoint)}`,
      parameter: param => param && {
        name: `${_chalk2.default.dim('name')}: ${_chalk2.default.cyan(param) || ''}`,
        description: `${_chalk2.default.dim('description')}: ${metadata.parameters[param].description || ''}`,
        example: `${_chalk2.default.dim('example')}: ${_chalk2.default.cyan.dim(metadata.parameters[param].example)}`
      },
      response: example => {
        const { exit_code, description } = example || '';
        let exampleLines = '';
        if (example && example.example) {
          exampleLines = example.example.split('\n').map(line => (0, _printTools.p)(12)(line)).join('\n');
        }

        return {
          mimetype: `${_chalk2.default.white('response')}: ${metadata.response.mimetype}`,
          description: example && `${_chalk2.default.dim('description')}: ${(0, _printTools.printCode)(exit_code, description)}`,
          exitCode: example && `${_chalk2.default.dim('exit code')}: ${(0, _printTools.printCode)(exit_code)}`,
          example: exampleLines && `${_chalk2.default.dim('example')}:\n${exampleLines}`
        };
      }
    };
  },
  handler: handler => {
    const metadata = handler.metadata;

    return handler && {
      name: `${_chalk2.default.white('event handler')}: ${_chalk2.default.cyan(handler.name)}`,
      description: `${_chalk2.default.dim('description')}: ${metadata.description || ''}`,
      parameter: param => param && {
        name: `${_chalk2.default.dim('name')}: ${_chalk2.default.cyan(param) || ''}`,
        description: `${_chalk2.default.dim('description')}: ${metadata.parameters[param].description || ''}`,
        example: `${_chalk2.default.dim('example')}: ${_chalk2.default.cyan.dim(metadata.parameters[param].example)}`
      }
    };
  },
  event: event => event && {
    name: `${_chalk2.default.white('event')}: ${_chalk2.default.cyan(event.name)}`,
    description: `${_chalk2.default.dim('description')}: ${event.metadata.description || ''}`,
    parameter: param => param && {
      name: `${_chalk2.default.dim('name')}: ${_chalk2.default.cyan(param) || ''}`,
      description: `${_chalk2.default.dim('description')}: ${event.metadata.parameters[param].description || ''}`,
      example: `${_chalk2.default.dim('example')}: ${_chalk2.default.black(event.metadata.parameters[param].example)}`
    }
  }
});

exports.default = socketListResponses;
module.exports = exports['default'];
//# sourceMappingURL=socket-list-responses.js.map