"use strict";

var _Layout = require("@enact/ui/Layout");
require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _Button = _interopRequireDefault(require("../../Button"));
var _DropManager = _interopRequireWildcard(require("../DropManager"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["containerShape"],
  _excluded2 = ["bottom", "center", "top"],
  _excluded3 = ["bottom", "center", "top"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var getElementClientCenter = function getElementClientCenter(element) {
  var _element$getBoundingC = element.getBoundingClientRect(),
    left = _element$getBoundingC.left,
    top = _element$getBoundingC.top,
    width = _element$getBoundingC.width,
    height = _element$getBoundingC.height;
  return {
    x: left + width / 2,
    y: top + height / 2
  };
};
describe('DropManager Specs', function () {
  var allSlotNames = ['bottom', 'center', 'top'];
  var DraggableCell = (0, _DropManager.Draggable)(_Layout.Cell);
  var containerShapes = {
    bottom: {
      orientation: 'landscape',
      edges: {
        bottom: true
      }
    },
    center: {
      orientation: 'portrait',
      edges: {
        top: false,
        bottom: false
      }
    },
    top: {
      orientation: 'landscape',
      edges: {
        top: true
      }
    }
  };
  var ResponsiveLayout = (0, _DropManager.ResponsiveBox)(function (_ref) {
    var containerShape = _ref.containerShape,
      rest = _objectWithoutProperties(_ref, _excluded);
    var orientation = containerShape.orientation === 'portrait' ? 'vertical' : 'horizontal';
    var axisAlign = 'space-evenly';
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Layout.Layout, _objectSpread({
      align: 'center ' + axisAlign,
      orientation: orientation
    }, rest));
  });
  var CustomResponsiveLayoutBase = function CustomResponsiveLayoutBase(_ref2) {
    var bottom = _ref2.bottom,
      center = _ref2.center,
      top = _ref2.top,
      rest = _objectWithoutProperties(_ref2, _excluded2);
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Layout.Layout, _objectSpread(_objectSpread({}, rest), {}, {
      orientation: "vertical",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(DraggableCell, {
        containerShape: containerShapes.top,
        name: "top",
        children: top
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(ResponsiveLayout, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(DraggableCell, {
          containerShape: containerShapes.center,
          name: "center",
          children: center
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(DraggableCell, {
        containerShape: containerShapes.bottom,
        name: "bottom",
        children: bottom
      })]
    }));
  };
  var CustomLayoutBase = function CustomLayoutBase(_ref3) {
    var bottom = _ref3.bottom,
      center = _ref3.center,
      top = _ref3.top,
      rest = _objectWithoutProperties(_ref3, _excluded3);
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Layout.Layout, _objectSpread(_objectSpread({}, rest), {}, {
      orientation: "vertical",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(DraggableCell, {
        containerShape: containerShapes.top,
        name: "top",
        children: top
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(DraggableCell, {
        containerShape: containerShapes.center,
        name: "center",
        children: center
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(DraggableCell, {
        containerShape: containerShapes.bottom,
        name: "bottom",
        children: bottom
      })]
    }));
  };
  var ResponsiveComponent = (0, _DropManager["default"])({
    slots: allSlotNames
  }, CustomResponsiveLayoutBase);
  var Component = (0, _DropManager["default"])({
    slots: allSlotNames
  }, CustomLayoutBase);
  test('should render `top`, `center` and `bottom` slots', function () {
    console.error = jest.fn(); // eslint-disable-line no-console
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(ResponsiveComponent, {
      arrangeable: true,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("top", {
        "data-testid": "top"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("center", {
        "data-testid": "center"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("bottom", {
        "data-testid": "bottom"
      })]
    }));
    var topSlot = _react.screen.getByTestId('top').parentElement;
    var centerSlot = _react.screen.getByTestId('center').parentElement;
    var bottomSlot = _react.screen.getByTestId('bottom').parentElement;
    expect(topSlot).toBeInTheDocument();
    expect(centerSlot).toBeInTheDocument();
    expect(bottomSlot).toBeInTheDocument();
  });
  test('should change `data-slot` value when changing `arrangement`', function () {
    var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(ResponsiveComponent, {
        arrangeable: true,
        arrangement: {
          bottom: 'bottom',
          center: 'center',
          top: 'top'
        },
        "data-testid": "dropManager",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("top", {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("center", {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("bottom", {})]
      })),
      rerender = _render.rerender;
    var topSlot = _react.screen.getByTestId('dropManager').children[0];
    expect(topSlot).toHaveAttribute('data-slot', 'top');
    rerender( /*#__PURE__*/(0, _jsxRuntime.jsxs)(ResponsiveComponent, {
      arrangeable: true,
      arrangement: {
        bottom: 'center',
        center: 'top',
        top: 'bottom'
      },
      "data-testid": "dropManager",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("top", {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("center", {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("bottom", {})]
    }));
    topSlot = _react.screen.getByTestId('dropManager').children[0];
    expect(topSlot).toHaveAttribute('data-slot', 'bottom');
  });
  test('should have `responsive layout` with `align-items: center` and `justify-content: space-evenly` style', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(ResponsiveComponent, {
      arrangeable: true,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("top", {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("center", {
        "data-testid": "center",
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(ResponsiveLayout, {
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Layout.Cell, {
            shrink: true,
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
              children: "1"
            })
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Layout.Cell, {
            shrink: true,
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
              children: "2"
            })
          })]
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("bottom", {})]
    }));
    var responsiveLayout = _react.screen.getByTestId('center').parentElement.parentElement;
    expect(responsiveLayout).toHaveStyle({
      alignItems: 'center',
      justifyContent: 'space-evenly'
    });
  });
  test('should rearrange items on touch', function () {
    var arrangement = {
      bottom: 'bottom',
      center: 'center',
      top: 'top'
    };
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(Component, {
      arrangeable: true,
      arrangement: arrangement,
      "data-testid": "dropManager",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("top", {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: "Drag me top"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("center", {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: "Drag me center"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("bottom", {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: "Drag me bottom"
        })
      })]
    }));
    var topSlot = _react.screen.getByTestId('dropManager').children[0];
    var centerSlot = _react.screen.getByTestId('dropManager').children[1];
    var bottomSlot = _react.screen.getByTestId('dropManager').children[2];
    topSlot.getBoundingClientRect = jest.fn(function () {
      return {
        width: 501,
        height: 100,
        top: 0,
        left: 0,
        bottom: 0,
        right: 0
      };
    });
    centerSlot.getBoundingClientRect = jest.fn(function () {
      return {
        width: 501,
        height: 100,
        top: 101,
        left: 0,
        bottom: 0,
        right: 0
      };
    });
    bottomSlot.getBoundingClientRect = jest.fn(function () {
      return {
        width: 501,
        height: 100,
        top: 202,
        left: 0,
        bottom: 0,
        right: 0
      };
    });
    var delta = {
      x: 0,
      y: 160
    };
    var from = getElementClientCenter(topSlot);
    var to = {
      x: from.x + delta.x,
      y: from.y + delta.y
    };
    var step = {
      x: to.x - from.x,
      y: to.y - from.y
    };
    var current = {
      clientX: from.x,
      clientY: from.y
    };
    _react.fireEvent.touchStart(topSlot, {
      touches: [current]
    });
    document.elementFromPoint = jest.fn(function () {
      return centerSlot;
    });
    current.clientX += step.x;
    current.clientY += step.y;
    _react.fireEvent.touchMove(topSlot, {
      changedTouches: [current]
    });
    document.elementFromPoint = jest.fn(function () {
      return bottomSlot;
    });
    current.clientX += step.x;
    current.clientY += step.y;
    _react.fireEvent.touchMove(topSlot, {
      changedTouches: [current]
    });
    _react.fireEvent.touchEnd(topSlot, {
      changedTouches: [current]
    });
    var firstChild = _react.screen.getByTestId('dropManager').children.item(0);
    var thirdChild = _react.screen.getByTestId('dropManager').children.item(2);
    expect(firstChild).toHaveAttribute('data-slot', 'bottom');
    expect(thirdChild).toHaveAttribute('data-slot', 'top');
  });
  test('should not rearrange items on touch when drop node is the same as the drag node', function () {
    var arrangement = {
      bottom: 'bottom',
      center: 'center',
      top: 'top'
    };
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(Component, {
      arrangeable: true,
      arrangement: arrangement,
      "data-testid": "dropManager",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("top", {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: "Drag me top"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("center", {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: "Drag me center"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("bottom", {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: "Drag me bottom"
        })
      })]
    }));
    var topSlot = _react.screen.getByTestId('dropManager').children[0];
    var centerSlot = _react.screen.getByTestId('dropManager').children[1];
    var bottomSlot = _react.screen.getByTestId('dropManager').children[2];
    topSlot.getBoundingClientRect = jest.fn(function () {
      return {
        width: 501,
        height: 100,
        top: 0,
        left: 0,
        bottom: 0,
        right: 0
      };
    });
    centerSlot.getBoundingClientRect = jest.fn(function () {
      return {
        width: 501,
        height: 100,
        top: 101,
        left: 0,
        bottom: 0,
        right: 0
      };
    });
    bottomSlot.getBoundingClientRect = jest.fn(function () {
      return {
        width: 501,
        height: 100,
        top: 202,
        left: 0,
        bottom: 0,
        right: 0
      };
    });
    var delta = {
      x: 0,
      y: 20
    };
    var from = getElementClientCenter(topSlot);
    var to = {
      x: from.x + delta.x,
      y: from.y + delta.y
    };
    var step = {
      x: to.x - from.x,
      y: to.y - from.y
    };
    var current = {
      clientX: from.x,
      clientY: from.y
    };
    _react.fireEvent.touchStart(topSlot, {
      touches: [current]
    });
    document.elementFromPoint = jest.fn(function () {
      // returning child of slotted element for code coverage purposes
      return topSlot.children[0];
    });
    current.clientX += step.x;
    current.clientY += step.y;
    _react.fireEvent.touchMove(topSlot, {
      changedTouches: [current]
    });
    _react.fireEvent.touchEnd(topSlot, {
      changedTouches: [current]
    });
    var firstChild = _react.screen.getByTestId('dropManager').children.item(0);
    var secondChild = _react.screen.getByTestId('dropManager').children.item(1);
    expect(firstChild).toHaveAttribute('data-slot', 'top');
    expect(secondChild).toHaveAttribute('data-slot', 'center');
  });
});