"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _default = {
  create(context) {
    const allowSameLine = context.options[0] && context.options[0].allowAllPropertiesOnSameLine;
    const messageId = allowSameLine ? 'propertiesOnNewlineAll' : 'propertiesOnNewline';
    const sourceCode = context.getSourceCode();
    return {
      ArrayPattern: node => {
        if (allowSameLine && node.elements.length > 1) {
          const firstTokenOfFirstProperty = sourceCode.getFirstToken(node.elements[0]);
          const lastTokenOfLastProperty = sourceCode.getLastToken(node.elements[node.properties.length - 1]);

          if (firstTokenOfFirstProperty.loc.end.line === lastTokenOfLastProperty.loc.start.line) {
            // All keys and values are on the same line
            return;
          }
        }

        for (let index = 1; index < node.elements.length; index++) {
          if (node.elements[index] === null || node.elements[index - 1] === null) {
            continue;
          }

          const lastTokenOfPreviousProperty = sourceCode.getLastToken(node.elements[index - 1]);
          const firstTokenOfCurrentProperty = sourceCode.getFirstToken(node.elements[index]);

          if (lastTokenOfPreviousProperty.loc.end.line === firstTokenOfCurrentProperty.loc.start.line) {
            context.report({
              fix(fixer) {
                const comma = sourceCode.getTokenBefore(firstTokenOfCurrentProperty);
                const rangeAfterComma = [comma.range[1], firstTokenOfCurrentProperty.range[0]]; // Don't perform a fix if there are any comments between the comma and the next property.

                if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) {
                  return null;
                }

                return fixer.replaceTextRange(rangeAfterComma, '\n');
              },

              loc: firstTokenOfCurrentProperty.loc,
              messageId,
              node
            });
          }
        }
      },
      ObjectPattern: node => {
        if (allowSameLine && node.properties.length > 1) {
          const firstTokenOfFirstProperty = sourceCode.getFirstToken(node.properties[0]);
          const lastTokenOfLastProperty = sourceCode.getLastToken(node.properties[node.properties.length - 1]);

          if (firstTokenOfFirstProperty.loc.end.line === lastTokenOfLastProperty.loc.start.line) {
            // All keys and values are on the same line
            return;
          }
        }

        for (let index = 1; index < node.properties.length; index++) {
          const lastTokenOfPreviousProperty = sourceCode.getLastToken(node.properties[index - 1]);
          const firstTokenOfCurrentProperty = sourceCode.getFirstToken(node.properties[index]);

          if (lastTokenOfPreviousProperty.loc.end.line === firstTokenOfCurrentProperty.loc.start.line) {
            context.report({
              fix(fixer) {
                const comma = sourceCode.getTokenBefore(firstTokenOfCurrentProperty);
                const rangeAfterComma = [comma.range[1], firstTokenOfCurrentProperty.range[0]]; // Don't perform a fix if there are any comments between the comma and the next property.

                if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) {
                  return null;
                }

                return fixer.replaceTextRange(rangeAfterComma, '\n');
              },

              loc: firstTokenOfCurrentProperty.loc,
              messageId,
              node
            });
          }
        }
      }
    };
  },

  meta: {
    fixable: 'whitespace',
    messages: {
      propertiesOnNewline: 'Destructuring properties must go on a new line.',
      propertiesOnNewlineAll: 'Destructuring properties must go on a new line if they aren\'t all on the same line.'
    },
    schema: [{
      additionalProperties: false,
      properties: {
        allowAllPropertiesOnSameLine: {
          default: false,
          type: 'boolean'
        }
      },
      type: 'object'
    }],
    type: 'layout'
  }
};
exports.default = _default;
module.exports = exports.default;