"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var breakpoints_exports = {};
__export(breakpoints_exports, {
  DEFAULT_BREAKPOINTS: () => DEFAULT_BREAKPOINTS,
  DefaultBreakpointID: () => DefaultBreakpointID,
  getBreakpoint: () => import_controls2.getBreakpoint,
  getInitialState: () => getInitialState,
  parseBreakpointsInput: () => parseBreakpointsInput,
  reducer: () => reducer
});
module.exports = __toCommonJS(breakpoints_exports);
var import_actions = require("../actions");
var import_controls2 = require("@makeswift/controls");
const DefaultBreakpointID = {
  Desktop: "desktop",
  Tablet: "tablet",
  Mobile: "mobile"
};
const DEFAULT_BREAKPOINTS = [
  {
    id: DefaultBreakpointID.Desktop,
    label: "Desktop",
    minWidth: 769
  },
  {
    id: DefaultBreakpointID.Tablet,
    label: "Tablet",
    minWidth: 576,
    maxWidth: 768,
    viewportWidth: 760
  },
  {
    id: DefaultBreakpointID.Mobile,
    label: "Mobile",
    maxWidth: 575,
    viewportWidth: 390
  }
];
function getInitialState(breakpoints = DEFAULT_BREAKPOINTS) {
  return breakpoints;
}
function reducer(state = getInitialState(), action) {
  if (!(0, import_actions.isKnownAction)(action))
    return state;
  switch (action.type) {
    case import_actions.ActionTypes.SET_BREAKPOINTS: {
      const breakpoints = action.payload.breakpoints;
      if (breakpoints.length === 0)
        throw new Error("Breakpoints cannot be empty.");
      return breakpoints;
    }
    default:
      return state;
  }
}
function parseBreakpointsInput(input) {
  validateBreakpointsInput(input);
  const sorted = Object.entries(input).map(([id, value]) => ({ ...value, id })).sort((a, b) => b.width - a.width);
  const transformed = sorted.reduce(
    (prev, curr, index, array) => {
      const { width, viewport, id, label } = curr;
      const next = array[index + 1];
      const breakpoint = {
        id,
        ...label && { label },
        ...next && { minWidth: next.width + 1 },
        maxWidth: width,
        viewportWidth: viewport ?? width
      };
      return [...prev, breakpoint];
    },
    [
      { id: DefaultBreakpointID.Desktop, label: "Desktop", minWidth: sorted[0].width + 1 }
    ]
  );
  return transformed;
}
function validateBreakpointsInput(input) {
  if (DefaultBreakpointID.Desktop in input) {
    throw new Error(
      `Cannot change the base breakpoint. "${DefaultBreakpointID.Desktop}" is reserved as the base breakpoint.`
    );
  }
  if (Object.keys(input).length === 0) {
    throw new Error(`Breakpoints cannot be empty. You must provide at least one breakpoint.`);
  }
  const sorted = Object.entries(input).map(([id, value]) => ({ ...value, id })).sort((a, b) => b.width - a.width);
  sorted.forEach(({ id, width, viewport }, index, array) => {
    if (viewport && viewport > width) {
      throw new Error(
        `Error on breakpoint "${id}". Viewport cannot be greater than its width.
        "${id}" has a viewport of ${viewport}px and a width of ${width}px.`
      );
    }
    const next = array[index + 1];
    if (viewport && next && viewport < next.width) {
      throw new Error(
        `Error on breakpoint "${id}". Viewport cannot be smaller than the next breakpoint's width.
        "${id}" has a viewport of ${viewport}px and the next breakpoint "${next.id}" has a width of ${next.width}px.`
      );
    }
    if (next && width === next.width) {
      throw new Error(
        `Breakpoints cannot have the same width. "${id}" and "${next.id}" have the same width`
      );
    }
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DEFAULT_BREAKPOINTS,
  DefaultBreakpointID,
  getBreakpoint,
  getInitialState,
  parseBreakpointsInput,
  reducer
});
//# sourceMappingURL=breakpoints.js.map