import createClassNameGen from './createClassNameGen';
import createCache from './createCache';

const defaultOpts = {};
const defaultCalculateRule = (prop, val) => `${prop}:${val};`;
const defaultMediaOrder = ['all'];
const getMedia = decl => decl.hasOwnProperty('media') ? decl.media : 'all';

export default function createInsertCSS(opts = defaultOpts) {
  const {
    calculateRule = defaultCalculateRule,
    mediaOrder = defaultMediaOrder,
    cache: cacheFromServer,
    lastClassName
  } = opts;

  const cache = createCache(
    cacheFromServer,
    createClassNameGen(lastClassName)
  );

  const sortMedia = (d1, d2) => mediaOrder.indexOf(getMedia(d1)) - mediaOrder.indexOf(getMedia(d2));

  return function insertCSS(styleSheet) {
    return function css(decls) {
      const result = cache.get(decls);

      if (result.hasOwnProperty('newDecls')) {
        for (const decl of (mediaOrder.length > 1 ? result.newDecls.sort(sortMedia) : result.newDecls)) {
          const rule = calculateRule(decl.prop, decl.value);

          styleSheet.insert(
            getMedia(decl),
            `.${decl.className}${decl.hasOwnProperty('sel') ? decl.sel : ''}`,
            rule
          );
        }

        if (process.env.NODE_ENV !== 'production') {
          for (const decl of result.newDecls) {
            const media = getMedia(decl);

            if (mediaOrder.indexOf(media) === -1) {
              console.warn(`Media query not found in \`mediaOrder\`: \"${media}\"`);
            }
          }
        }
      }

      return result.className;
    }
  }
}

@@ -1020,24 +1020,258 @@
 cl.value);%0A%0A
+          if (process.env.NODE_ENV !== 'production') %7B%0A            if (typeof rule !== 'string') %7B%0A              throw new TypeError(%60Expected %5C%60calculateRule()%5C%60 to return a string. It returned $%7Brule%7D.%60);%0A            %7D%0A          %7D%0A%0A
           st

Add hint